Re: array/Array: "hard" bounds checking

2018-02-21 Thread TheFlyingFiddle via Digitalmars-d-learn
On Thursday, 22 February 2018 at 00:34:59 UTC, kdevel wrote: Is there a D equivalent of the C++ at method? I would like to reformulate repro2.d --- void main () { import std.stdio; import std.container; import std.range; auto z = Array!char(); z.reserve(0xC000_);

Re: Tuts/Aritcles: Incrementasl C++-to-D conversion?

2018-02-21 Thread Mike Franklin via Digitalmars-d-learn
On Thursday, 22 February 2018 at 04:16:44 UTC, Nick Sabalausky (Abscissa) wrote: Are there any tutorials or articles out there for "getting started with converting a C++ codebase to D one module at a time?" Or at the very least: tips, tricks, lessions learned, from those who have come before.

Tuts/Aritcles: Incrementasl C++-to-D conversion?

2018-02-21 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn
Are there any tutorials or articles out there for "getting started with converting a C++ codebase to D one module at a time?" Or at the very least: tips, tricks, lessions learned, from those who have come before.

Re: Negative index range violation

2018-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/21/18 7:30 PM, SrMordred wrote: But with a slice negative indexes are never allowed, even on a pointer. youd have to do (c-1)[0 .. 1]; Nice! Thank you both! In D Slice article it says "You can even use negative indexes!" so I thought that the [-1..x] should work too :) Hah! I

Re: Negative index range violation

2018-02-21 Thread SrMordred via Digitalmars-d-learn
But with a slice negative indexes are never allowed, even on a pointer. youd have to do (c-1)[0 .. 1]; Nice! Thank you both! In D Slice article it says "You can even use negative indexes!" so I thought that the [-1..x] should work too :)

array/Array: "hard" bounds checking

2018-02-21 Thread kdevel via Digitalmars-d-learn
Is there a D equivalent of the C++ at method? I would like to reformulate repro2.d --- void main () { import std.stdio; import std.container; import std.range; auto z = Array!char(); z.reserve(0xC000_); z.capacity.writeln; z.length.writeln; for (uint u = 0; u <

Re: Negative index range violation

2018-02-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote: string x = "123"; auto c = x.ptr; c++; writeln(c[-1]); // 1 That's only happening because pointers bypass range checks. writeln(c[-1..0]); //BOOM Range violation But with a slice negative indexes are never allowed, even on a

Re: Negative index range violation

2018-02-21 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote: string x = "123"; auto c = x.ptr; c++; writeln(c[-1]); // 1 writeln(c[-1..0]); //BOOM Range violation Can I do this / Bug / some mistake ? youd have to do (c-1)[0 .. 1];

Negative index range violation

2018-02-21 Thread SrMordred via Digitalmars-d-learn
string x = "123"; auto c = x.ptr; c++; writeln(c[-1]); // 1 writeln(c[-1..0]); //BOOM Range violation Can I do this / Bug / some mistake ?

Re: Going from string to identifier

2018-02-21 Thread Basile B. via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 22:22:55 UTC, Meta wrote: (I'm not sure if the available compiler implementations actually enforce this) Yes it does, example ``` enum s = q{€}; ``` gives: `Error: character 0x20ac is not a valid token`

Re: Going from string to identifier

2018-02-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 22:22:55 UTC, Meta wrote: Mixins have to be full declarations. They need to be full declarations, full statements, or full expressions, depending on the context where they appear. string s = mixin("teste"); so that is a full expression and thus

Re: Going from string to identifier

2018-02-21 Thread Meta via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 22:11:04 UTC, Jean-Louis Leroy wrote: Here's what I am trying to do: mixin template MakeFun(string ID, int X) { int mixin(ID)() { return X; } } mixin MakeFun!("one", 1); // int one() { return 1; } Alas I get: makefunc.d(3): Error: no identifier for

Going from string to identifier

2018-02-21 Thread Jean-Louis Leroy via Digitalmars-d-learn
Here's what I am trying to do: mixin template MakeFun(string ID, int X) { int mixin(ID)() { return X; } } mixin MakeFun!("one", 1); // int one() { return 1; } Alas I get: makefunc.d(3): Error: no identifier for declarator `int` makefunc.d(3): Error: found `{` when expecting `;`

Re: Alias vs templates

2018-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/21/18 1:46 PM, Jean-Louis Leroy wrote: I am trying to figure out a crispier syntax for templatized open methods. I am stumbling on this (see comments): // dmd -run conflict.d int foo(); struct Foo {   static int foo(int x) { return x; } } alias foo = Foo.foo; // overload with an alias

Re: Alias vs templates

2018-02-21 Thread Jean-Louis Leroy via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 20:27:29 UTC, Steven Schveighoffer wrote: On 2/21/18 1:46 PM, Jean-Louis Leroy wrote: [...] I think because one is a function, which is allowed to be overloaded, the other is a symbol. But clearly, there are some liberties taken when you are overloading

Alias vs templates

2018-02-21 Thread Jean-Louis Leroy via Digitalmars-d-learn
I am trying to figure out a crispier syntax for templatized open methods. I am stumbling on this (see comments): // dmd -run conflict.d int foo(); struct Foo { static int foo(int x) { return x; } } alias foo = Foo.foo; // overload with an alias - OK int bar(T)(); int bar(T)(T x) { return

Re: return type based on content of an array

2018-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/21/18 1:45 AM, thorstein wrote: Hi, I'm going circles... ;) I read a string that contains an array of unknown dimension like: a = [1,2,3,4] or a = [[1,2],[3,4]] or a = [[[1,2],[3,4]],[[5,6],[7,8]]] With that I want to perform specified operations e.g. also provided on command line.

Re: Destructing Struct

2018-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/21/18 6:24 AM, Jiyan wrote: I think i found my solution: is it __xdtor? :P Yes, that is the function that will run *recursively* all the destructors (just __dtor runs the destructor method if you provided one). But I'd recommend as the others did, using destroy. -Steve

Re: Manually allocating a File

2018-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/20/18 9:59 PM, Nicholas Wilson wrote: FYI, File is a reference counted FILE* so theres not really any point of heap allocating it. More FYI, the reference counted payload is actually allocated on the C heap :) So you are wasting a lot of effort to do something that is already done.

Re: How to instantiate a template struct with a template constructor without relying on auto deduction?

2018-02-21 Thread ParticlePeter via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 14:29:31 UTC, Simen Kjærås wrote: On Wednesday, 21 February 2018 at 14:11:10 UTC, ParticlePeter wrote: struct Foo(T) { T bar; this(S)(S s) { bar = convert(s); } } auto foo = Foo!int(some_float); this works because S is deduced as

Re: Can someone help a Reddit user

2018-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/20/18 2:52 PM, Steven Schveighoffer wrote: On 2/20/18 2:00 PM, bachmeier wrote: Someone has posted a question on our subreddit. Would be nice if he could get an answer: https://www.reddit.com/r/d_language/comments/7yxwvm/why_do_my_threads_write_to_the_wrong_file/ I responded. Looks to

Re: How to instantiate a template struct with a template constructor without relying on auto deduction?

2018-02-21 Thread ixid via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 14:42:56 UTC, Simen Kjærås wrote: On Wednesday, 21 February 2018 at 14:29:38 UTC, ixid wrote: I do not understand what is happening here, I tried to wrote what I thought would be the answer. If someone could explain that would be great. I wrote this code:

Re: Building from source on FreeBSD

2018-02-21 Thread psychoticRabbit via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 11:41:18 UTC, Diederik de Groot wrote: Removing the pragma(lib, "curl") seems to fix the issue on DFly (and FreeBSD). Updated the pull request. I guess pragma(lib, xxx) needs a little bit of attention to see what causes it not to work. something to do with

Re: How to instantiate a template struct with a template constructor without relying on auto deduction?

2018-02-21 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 14:29:38 UTC, ixid wrote: I do not understand what is happening here, I tried to wrote what I thought would be the answer. If someone could explain that would be great. I wrote this code: struct Foo2(T, S) { T bar; this(S s) { bar = s.to!T; } }

Re: How to instantiate a template struct with a template constructor without relying on auto deduction?

2018-02-21 Thread ixid via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 14:11:10 UTC, ParticlePeter wrote: struct Foo(T) { T bar; this(S)(S s) { bar = convert(s); } } auto foo = Foo!int(some_float); this works because S is deduced as typeof(some_float), but how would I instantiate the struct without relying on auto

Re: How to instantiate a template struct with a template constructor without relying on auto deduction?

2018-02-21 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 14:11:10 UTC, ParticlePeter wrote: struct Foo(T) { T bar; this(S)(S s) { bar = convert(s); } } auto foo = Foo!int(some_float); this works because S is deduced as typeof(some_float), but how would I instantiate the struct without relying on auto

How to instantiate a template struct with a template constructor without relying on auto deduction?

2018-02-21 Thread ParticlePeter via Digitalmars-d-learn
struct Foo(T) { T bar; this(S)(S s) { bar = convert(s); } } auto foo = Foo!int(some_float); this works because S is deduced as typeof(some_float), but how would I instantiate the struct without relying on auto deduction? Suppose we would have this kind of constructor where auto

Re: Destructing Struct

2018-02-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 12:07:47 UTC, ketmar wrote: `p.destroy` will call the dtors for you. So it is the same function but I prefer to always write it: .destroy(p); yes, a leading dot. This ensures you call the top-level destroy function instead of any members which may not do

Re: Destructing Struct

2018-02-21 Thread ketmar via Digitalmars-d-learn
Jiyan wrote: Hi :), What i thought was that when i create a struct dynamically i can just deconstruct it with __dtor lets say: struct U {...} struct S {... private U _member;} S* p; p = cast(S*)malloc(S.sizeof); // just run that if it compiles, for simplicity // we dont use

Re: Building from source on FreeBSD

2018-02-21 Thread Diederik de Groot via Digitalmars-d-learn
On Tuesday, 20 February 2018 at 03:59:06 UTC, psychoticRabbit wrote: On Monday, 19 February 2018 at 12:01:31 UTC, Jonathan M Davis wrote: ok... so I decided to dig into it a little further. seems the problem relates to a single line, in dget.d pragma(lib, "curl"); I just commented out

Re: Destructing Struct

2018-02-21 Thread Jiyan via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 11:12:01 UTC, Jiyan wrote: Hi :), What i thought was that when i create a struct dynamically i can just deconstruct it with __dtor lets say: struct U {...} struct S {... private U _member;} S* p; p = cast(S*)malloc(S.sizeof); // just run that if it

Destructing Struct

2018-02-21 Thread Jiyan via Digitalmars-d-learn
Hi :), What i thought was that when i create a struct dynamically i can just deconstruct it with __dtor lets say: struct U {...} struct S {... private U _member;} S* p; p = cast(S*)malloc(S.sizeof); // just run that if it compiles, for simplicity // we dont use __traits(compiles, ...)

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread Kagamin via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 10:43:14 UTC, 0x wrote: Gotta save this too. [BTW how do I post a thumbs up emoji] 

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread rikki cattermole via Digitalmars-d-learn
On 21/02/2018 10:43 AM, 0x wrote: On Wednesday, 21 February 2018 at 10:24:39 UTC, ketmar wrote: 0x wrote: [...] and that is exactly what slices are for! ;-) you are probably better to use `const(char)[]`, tho. like this: // don't store `s`, as it's contents may change

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread 0xFFFFFFFF via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 10:24:39 UTC, ketmar wrote: 0x wrote: [...] and that is exactly what slices are for! ;-) you are probably better to use `const(char)[]`, tho. like this: // don't store `s`, as it's contents may change after exiting of `myfunc` // (this

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread 0xFFFFFFFF via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm getting back to D after

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread rikki cattermole via Digitalmars-d-learn
On 21/02/2018 10:41 AM, 0x wrote: On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero)

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread 0xFFFFFFFF via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 10:22:56 UTC, rikki cattermole wrote: On 21/02/2018 10:17 AM, 0x wrote: On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread 0xFFFFFFFF via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 10:23:23 UTC, Jonathan M Davis wrote: As I said in my previous response, read this article: https://dlang.org/articles/d-array-article.html Sorry, I didn't see this before I replied. Had I, I wouldn't have done that. string sv = "some string"; then _zero_

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread ketmar via Digitalmars-d-learn
0x wrote: On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread rikki cattermole via Digitalmars-d-learn
On 21/02/2018 10:17 AM, 0x wrote: On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero)

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, February 21, 2018 10:17:55 0x via Digitalmars-d-learn wrote: > On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: > > What is the equivalent of C++17 std::string_view (an object > > that can refer to a constant contiguous sequence of char-like > > objects with

C++ std::string_view equivalent in D?

2018-02-21 Thread 0xFFFFFFFF via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm getting back to D after

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, February 21, 2018 09:21:58 0x via Digitalmars-d-learn wrote: > What is the equivalent of C++17 std::string_view (an object that > can refer to a constant contiguous sequence of char-like objects > with the first element of the sequence at position zero) in D? > > PS: I'm

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread Kagamin via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm getting back to D after

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread Smaehtin via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm getting back to D after

Re: C++ std::string_view equivalent in D?

2018-02-21 Thread rikki cattermole via Digitalmars-d-learn
On 21/02/2018 9:21 AM, 0x wrote: What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm getting back to D after years (since DMD 1 days). A

C++ std::string_view equivalent in D?

2018-02-21 Thread 0xFFFFFFFF via Digitalmars-d-learn
What is the equivalent of C++17 std::string_view (an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero) in D? PS: I'm getting back to D after years (since DMD 1 days). A lot changes since v1.0.

Re: return type based on content of an array

2018-02-21 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 21 February 2018 at 06:45:14 UTC, thorstein wrote: Hi, I'm going circles... ;) I read a string that contains an array of unknown dimension like: a = [1,2,3,4] or a = [[1,2],[3,4]] or a = [[[1,2],[3,4]],[[5,6],[7,8]]] With that I want to perform specified operations e.g. also