Re: array/Array: "hard" bounds checking
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_); z.capacity.writeln; z.length.writeln; for (uint u = 0; u < 0xC000_; ++u) z.insert = 'Y'; int i = -1073741825; i.writeln; z[i] = 'Q'; z[i].writeln; } --- $ dmd -O -m32 repro2.d $ ./repro2 3221225472 0 -1073741825 Q such that it fails like the 64 bit version: $ dmd -O -m64 repro2.d $ ./repro2 3221225472 0 -1073741825 core.exception.RangeError@.../dmd2/linux/bin64/../../src/phobos/std/container/array.d(650): Range violation ??:? _d_arrayboundsp [0x440d22] .../dmd2/linux/bin64/../../src/phobos/std/container/array.d:650 inout pure nothrow ref @nogc @safe inout(char) std.container.array.Array!(char).Array.opIndex(ulong) [0x43bb0f] repro2.d:14 _Dmain [0x43afff] Well in a 32bit program the value 0xBFFF_(-1073741825) is clearly inside the array. The Array class uses an size_t internaly for storing the length/capacity, that is uint in a 32bit program and ulong in a 64bit program. In the 64bit the value (0x__BFFF_)(-1073741825) is larger than 0xC000_000 so it will be out of bounds in this case. If you want any negative integer to be out of bounds the capacity cannot be larger than 0x7FFF_ in 32bit programs. But this behavior is strange. Well the really strange/bad part is that it's allowed by the compiler in the first place. I would be very happy if a user was forced to make an explicit cast for int <-> uint conversions. Like we have to do for long -> int conversions. Also signed/unsigned comparisons should be strictly outlawed by the compiler. Eg: uint a = 3; int b = -1; assert(a > b); //No idea what should happen here.
Re: Tuts/Aritcles: Incrementasl C++-to-D conversion?
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. These are the resources I'm aware of: https://dlang.org/articles/cpptod.html https://wiki.dlang.org/Programming_in_D_for_C%2B%2B_Programmers Mike
Tuts/Aritcles: Incrementasl C++-to-D conversion?
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
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 never thought of doing a slice with negative indexes ;) Note that the statement is about C pointers, so since C doesn't have slicing, it stands to reason that slicing with negative indexes isn't supported. -Steve
Re: Negative index range violation
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
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 < 0xC000_; ++u) z.insert = 'Y'; int i = -1073741825; i.writeln; z[i] = 'Q'; z[i].writeln; } --- $ dmd -O -m32 repro2.d $ ./repro2 3221225472 0 -1073741825 Q such that it fails like the 64 bit version: $ dmd -O -m64 repro2.d $ ./repro2 3221225472 0 -1073741825 core.exception.RangeError@.../dmd2/linux/bin64/../../src/phobos/std/container/array.d(650): Range violation ??:? _d_arrayboundsp [0x440d22] .../dmd2/linux/bin64/../../src/phobos/std/container/array.d:650 inout pure nothrow ref @nogc @safe inout(char) std.container.array.Array!(char).Array.opIndex(ulong) [0x43bb0f] repro2.d:14 _Dmain [0x43afff]
Re: Negative index range violation
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 pointer.
Re: Negative index range violation
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
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
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
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 legal.
Re: Going from string to identifier
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 declarator `int` makefunc.d(3): Error: found `{` when expecting `;` makefunc.d(3): Error: declaration expected, not `return` makefunc.d(4): Error: unrecognized declaration Is there a shorter way than building the entire function definition as a string mixin? As in: mixin template MakeFun(string ID, int X) { import std.format; mixin(format("int %s() { return %s; }", ID, X)); } mixin MakeFun!("one", 1); Mixins have to be full declarations. You can't mix in bits and pieces... except when you can: import std.stdio; void main() { enum teste = "asdf"; string s = mixin("teste"); writeln(s); //Prints "asdf" } It looks like a grammar error as opposed to a semantic one. D's grammar just doesn't support `mixin` in the function name position. One way you can make it a little more palateable: mixin template MakeFun(string ID, int X) { import std.format; mixin(q{ int %s { return %s; } }.format(ID, X)); } `q{}` denotes a token string that must contain valid tokens (I'm not sure if the available compiler implementations actually enforce this), and I _think_ token strings will be properly syntax-highlighted by most tools. https://dlang.org/spec/lex.html#token_strings
Going from string to identifier
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 `;` makefunc.d(3): Error: declaration expected, not `return` makefunc.d(4): Error: unrecognized declaration Is there a shorter way than building the entire function definition as a string mixin? As in: mixin template MakeFun(string ID, int X) { import std.format; mixin(format("int %s() { return %s; }", ID, X)); } mixin MakeFun!("one", 1);
Re: Alias vs templates
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 - OK int bar(T)(); int bar(T)(T x) { return x; } // overloaded function templates - OK int baz(T)(); struct Baz { static int baz(T)(T x) { return x; } } //alias baz = Baz.baz; // Error: alias conflict.baz conflicts with template conflict.baz(T)() at conflict.d(11) void main() { import std.stdio; writeln(foo(42)); // 42 writeln(bar(666)); // 666 } Why? 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 function templates, as overloading them works just fine. I think this may have been a patch at some point (you didn't use to be able to overload template functions with normal functions). It seems like a reasonable request that this code should work. -Steve
Re: Alias vs templates
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 function templates, as overloading them works just fine. I think this may have been a patch at some point (you didn't use to be able to overload template functions with normal functions). It seems like a reasonable request that this code should work. I just discovered that this works: int baz(T)(); struct Baz { static int baz(T)(T x) { return x; } } alias baz(T) = Baz.baz!T;
Alias vs templates
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 x; } // overloaded function templates - OK int baz(T)(); struct Baz { static int baz(T)(T x) { return x; } } //alias baz = Baz.baz; // Error: alias conflict.baz conflicts with template conflict.baz(T)() at conflict.d(11) void main() { import std.stdio; writeln(foo(42)); // 42 writeln(bar(666)); // 666 } Why?
Re: return type based on content of an array
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. Because at compile time the dimension of the array is unkown I did not find any possibility to do something like: auto arr = func(a); where 'auto func(string str)' should return either a 1D, 2D, 3D array and arr is not locked in an if(){} scope. Is there any way and What should I'm looking for? If you want a specific type that the type system cares about, you have to implement every possibility. Then you can use Variant. But if you want a recursive strategy, where you need the dimensions at runtime only, then you can write a struct with a tagged union which gives you all the info you need: struct NDimArray { size_t dimensions; // if 0, then this is a value union { NDimArray[] elements; int value; } } You can probably figure out the rest. Probably you can eliminate the dimensions field somehow to save space. This is how things like JSON DOM structures work. -Steve
Re: Destructing Struct
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
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. -Steve
Re: How to instantiate a template struct with a template constructor without relying on auto deduction?
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 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 deduction is not possible: this(int n)(float f) { static foreach( i; 0..n) { do_some_ctfe_magic;} } How to instantiate Foo then? No can do. The solution is to use a factory function: Feared the same, thanks. struct Foo(T) { static Foo create(int n)(float f) { Foo result; static foreach( i; 0..n) { do_some_ctfe_magic;} return result; } } -- Simen I will consider this, actually I use something quite close, but my create is not static and does not return anything. It simply initializes the struct after it has been constructed with the default ctor. The templated user ctor would have been nice, though.
Re: Can someone help a Reddit user
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 me like a race condition in the DMC libc code, but I don't have an environment set up to test at the moment. Wow, I didn't realize how bad DMC's FILE * was in terms of thread safety: http://bugzilla.digitalmars.com/issues/show_bug.cgi?id=327 I would highly recommend avoiding I/O on Win32, unless you are using Visual Studio libc, or using Windows handles directly. I can't believe it has been this bad and we haven't had mountains of bug reports on it. -Steve
Re: How to instantiate a template struct with a template constructor without relying on auto deduction?
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: struct Foo2(T, S) { T bar; this(S s) { bar = s.to!T; } } void main() { float some_float = 0.5f; int some_int = 1; auto foo1 = Foo2!(int, float)(some_float);// Compiles, OK! auto foo2 = Foo2!(int, float)(some_int); // Compiles, wat? } int n = 1; float f = n; // Basically this. Foo2!(int, float) expects a float, and ints are implicitly convertible to float. -- Simen Ah yes, that was silly of me to forget. Thanks!
Re: Building from source on FreeBSD
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 this perhaps? https://stackoverflow.com/questions/23214697/trouble-with-curl-and-pragma in any case, that line doesn't even need to be there AFAICT. dget builds and works just fine with it commented out. I've commented it out on my linux builds too.
Re: How to instantiate a template struct with a template constructor without relying on auto deduction?
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; } } void main() { float some_float = 0.5f; int some_int = 1; auto foo1 = Foo2!(int, float)(some_float);// Compiles, OK! auto foo2 = Foo2!(int, float)(some_int); // Compiles, wat? } int n = 1; float f = n; // Basically this. Foo2!(int, float) expects a float, and ints are implicitly convertible to float. -- Simen
Re: How to instantiate a template struct with a template constructor without relying on auto deduction?
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 deduction? Suppose we would have this kind of constructor where auto deduction is not possible: this(int n)(float f) { static foreach( i; 0..n) { do_some_ctfe_magic;} } How to instantiate Foo then? 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; } } void main() { float some_float = 0.5f; int some_int = 1; auto foo1 = Foo2!(int, float)(some_float);// Compiles, OK! auto foo2 = Foo2!(int, float)(some_int); // Compiles, wat? }
Re: How to instantiate a template struct with a template constructor without relying on auto deduction?
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 deduction? Suppose we would have this kind of constructor where auto deduction is not possible: this(int n)(float f) { static foreach( i; 0..n) { do_some_ctfe_magic;} } How to instantiate Foo then? No can do. The solution is to use a factory function: struct Foo(T) { static Foo create(int n)(float f) { Foo result; static foreach( i; 0..n) { do_some_ctfe_magic;} return result; } } -- Simen
How to instantiate a template struct with a template constructor without relying on auto deduction?
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 deduction is not possible: this(int n)(float f) { static foreach( i; 0..n) { do_some_ctfe_magic;} } How to instantiate Foo then?
Re: Destructing Struct
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 the same thing.
Re: Destructing Struct
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 __traits(compiles, ...) p.__dtor; The thing here is that this doesn't work because of when S has an element that that is private and has a __dtor itself, the __dtor from U doesnt get called before the call of __dtor from S - or after. Is there any way with traits or sth to do that? Are delete, destroy or any other functions the standard library working here? I would prefer a solution that can be build by myself - so without the standard library for example with traits. Thanks :) `p.destroy` will call the dtors for you. you'd better not use `__`-frefixed symbols yourself, as they aren't actually a part of a language, they're just implementation details.
Re: Building from source on FreeBSD
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 the one line in dget.d, and it all seems to build ok (including dget). So no need to comment out tools in posix.mak anymore. 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.
Re: Destructing Struct
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 compiles, for simplicity // we dont use __traits(compiles, ...) p.__dtor; The thing here is that this doesn't work because of when S has an element that that is private and has a __dtor itself, the __dtor from U doesnt get called before the call of __dtor from S - or after. Is there any way with traits or sth to do that? Are delete, destroy or any other functions the standard library working here? I would prefer a solution that can be build by myself - so without the standard library for example with traits. Thanks :) I think i found my solution: is it __xdtor? :P
Destructing Struct
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, ...) p.__dtor; The thing here is that this doesn't work because of when S has an element that that is private and has a __dtor itself, the __dtor from U doesnt get called before the call of __dtor from S - or after. Is there any way with traits or sth to do that? Are delete, destroy or any other functions the standard library working here? I would prefer a solution that can be build by myself - so without the standard library for example with traits. Thanks :)
Re: C++ std::string_view equivalent in D?
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?
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 after exiting of `myfunc` // (this is what `const` implies here) void myfunc (const(char)[] s) { ... } ... string s = "test string"; myfunc(s); // yep, this works s = s[2..4]; // this doesn't allocate myfunc(s); myfunc(s[3..6]); // or this, it doesn't allocate myfunc("abc"); // or this, doesn't allocate you got the idea. Gotta save this too. [BTW how do I post a thumbs up emoji] +X (typically just 1) means that you agree. But here we just say "thanks". This "forum" is backed by an NNTP server, so lots of the more newer concepts don't exist here.
Re: C++ std::string_view equivalent in D?
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 is what `const` implies here) void myfunc (const(char)[] s) { ... } ... string s = "test string"; myfunc(s); // yep, this works s = s[2..4]; // this doesn't allocate myfunc(s); myfunc(s[3..6]); // or this, it doesn't allocate myfunc("abc"); // or this, doesn't allocate you got the idea. Gotta save this too. [BTW how do I post a thumbs up emoji]
Re: C++ std::string_view equivalent in D?
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 years (since DMD 1 days). A lot changes since v1.0. Thanks guys @all. I completed the task. Glad to know there are lots people helping out here.
Re: C++ std::string_view equivalent in D?
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) in D? PS: I'm getting back to D after years (since DMD 1 days). A lot changes since v1.0. Thanks guys @all. I completed the task. Glad to know there are lots people helping out here. Hop on to IRC (FreeNode #d) or Discord https://discord.gg/sVMHUD (invite expires in a day) if you'd like a more interactive conversation :)
Re: C++ std::string_view equivalent in D?
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 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. Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` string sv = "some string"; assert(sv.length == 11); Or just "some string".length ;) Thanks, it took time before I updated my reply to prevous answers. I didn't see these before I did. [I'm not that dumb] :winks:
Re: C++ std::string_view equivalent in D?
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_ heap allocations take place. I think u've answered my question.
Re: C++ std::string_view equivalent in D?
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 getting back to D after years (since DMD 1 days). A lot changes since v1.0. Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` So I can query it's size, make it point to sth else, use it in callbacks etc. Of course I'm aware of scoping etc... I'm NOT doing any dynamic allocations, I just want a situation where a string allocation string would be a little bit expensive. 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 is what `const` implies here) void myfunc (const(char)[] s) { ... } ... string s = "test string"; myfunc(s); // yep, this works s = s[2..4]; // this doesn't allocate myfunc(s); myfunc(s[3..6]); // or this, it doesn't allocate myfunc("abc"); // or this, doesn't allocate you got the idea.
Re: C++ std::string_view equivalent in D?
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) in D? PS: I'm getting back to D after years (since DMD 1 days). A lot changes since v1.0. Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` string sv = "some string"; assert(sv.length == 11); Or just "some string".length ;)
Re: C++ std::string_view equivalent in D?
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 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. > > Wow! Thanks guys. > Those are handy, but I specifically want to do something like: > > ``` > string_view sv = "some string"; > ``` > So I can query it's size, make it point to sth else, use it in > callbacks etc. Of course I'm aware of scoping etc... > > I'm NOT doing any dynamic allocations, I just want a situation > where a string allocation string would be a little bit expensive. > > > [I believe I still have to study D more in that case to come up > with sth better, enjoying my ride so far] As I said in my previous response, read this article: https://dlang.org/articles/d-array-article.html You clearly do not understand at all how strings work in D. Strings in D basically _are_ string_views where most strings have the actual memory buffer managed by the GC. If you do string sv = "some string"; then _zero_ heap allocations take place. - Jonathan M Davis
C++ std::string_view equivalent in D?
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 years (since DMD 1 days). A lot changes since v1.0. Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` So I can query it's size, make it point to sth else, use it in callbacks etc. Of course I'm aware of scoping etc... I'm NOT doing any dynamic allocations, I just want a situation where a string allocation string would be a little bit expensive. [I believe I still have to study D more in that case to come up with sth better, enjoying my ride so far]
Re: C++ std::string_view equivalent in D?
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 getting back to D after years (since DMD 1 days). A lot > changes since v1.0. strings in D are that way by their very nature, because they're dynamic arrays, and in D, dynamic arrays are just poiner and a length. Effectively, a dynamic array is struct DynamicArray(T) { size_t length; T* ptr; } They're slices of some piece of memory - usually GC-allocated memory, though you can slice any memory and get a dynamic array out of it. I'd suggest reading this article: https://dlang.org/articles/d-array-article.html It does not use the official terminology, because it refers to the GC-managed buffer as the dynamic array rather than the T[] as being the dynamic array (whereas the official terminology is that T[] is a dynamic array, regardless of what memory it's a slice of), but otherwise, what it says is quite good and should give you a solid idea of how arrays work in D. - Jonathan M Davis
Re: C++ std::string_view equivalent in D?
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 years (since DMD 1 days). A lot changes since v1.0. Strings were always slices in D even in version 1.0, what's changed is that string data is immutable now - guaranteed to not change.
Re: C++ std::string_view equivalent in D?
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 years (since DMD 1 days). A lot changes since v1.0. Perhaps you are looking for slices: https://dlang.org/spec/arrays.html#slicing?
Re: C++ std::string_view equivalent in D?
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 lot changes since v1.0. Think of string_view as a poor man's slice support. Slices are essentially just dynamic arrays, a length and a pointer. char* ptr = cast(char*)malloc(32); char[] array = ptr[0 .. 32]; char[] anotherArray = array[16 .. $]; assert(anotherArray.length == 16); It isn't limited to just char's either :)
C++ std::string_view equivalent in D?
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
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 provided on command line. Because at compile time the dimension of the array is unkown I did not find any possibility to do something like: auto arr = func(a); where 'auto func(string str)' should return either a 1D, 2D, 3D array and arr is not locked in an if(){} scope. Is there any way and What should I'm looking for? Thanks, thorstein There's std.variant: https://dlang.org/phobos/std_variant The code would probably be something like: Algebraic!(int[], int[][], int[][][]) array; if (value.is1D) { array = value.read1DArray(); } else if (value.is2D) { array = value.read2DArray(); } else { array = value.read3DArray(); } And you'd grab the type of value you want by array.get!(int[][]). -- Simen