Re: How to know if opSlice is defined for a type (including built-in types)?
On Wednesday, 20 January 2016 at 15:25:10 UTC, Jonathan M Davis wrote: On Wednesday, January 20, 2016 13:06:00 chardetm via Digitalmars-d-learn wrote: Anyone who has the same problem: I found std.range.primitives.hasSlicing (https://dlang.org/phobos/std_range_primitives.html#hasSlicing) which does exactly what I want! Note that because strings are treated as ranges of dchar regardless of what their actual character type is, arrays of char and wchar (so-called "narrow" strings) are not consider to have slicing or random access by the traits in std.range. So, hasSlicing!string is false, though for anything other than an array of char or wchar, it will do what you're looking for, whereas for arrays of char or wchar, you really shouldn't be using the slice operator on them without knowing that they're what you're operating on so that you take the Unicode issues into account correctly. - Jonathan M Davis Yes and that was the next step of my problem, it turned out that it was already taken into account by hasSlicing! Thank you very much!
Re: How to know if opSlice is defined for a type (including built-in types)?
On Wednesday, 20 January 2016 at 10:44:36 UTC, Rikki Cattermole wrote: template CanSlice(T) { enum CanSlice = __traits(compiles, {T t; auto v = t[0 .. 1];}) || __traits(compiles, {T t; auto v = t.opSlice();}); } Should work. Thanks it works just fine!
How to know if opSlice is defined for a type (including built-in types)?
Hi everyone, I was wondering if it was possible to know at compile time if opSlice is defined for a type, including built-in types like "string". Here is the code I have: import std.stdio; struct Test { Test opSlice(size_t i, size_t j) { return this; // We don't care what it returns } } void main () { string s = "Hello"; auto s2 = s[0..2]; static if (is(typeof())) writeln("Ok string"); Test t; auto t2 = t[0..2]; static if (is(typeof())) writeln("Ok Test"); } Output: Ok Test How to make it work for "string" too (if possible)? And is there a way to differentiate "opSlice()" from "opSlice(size_t, size_t)" (or "opSlice(T, T)" for any T)? Thank you in advance!
Re: How to know if opSlice is defined for a type (including built-in types)?
Anyone who has the same problem: I found std.range.primitives.hasSlicing (https://dlang.org/phobos/std_range_primitives.html#hasSlicing) which does exactly what I want!
Re: fromStringz problem with gdc
On Monday, 6 April 2015 at 17:55:42 UTC, Iain Buclaw wrote: On Monday, 6 April 2015 at 17:47:27 UTC, chardetm wrote: Hello everyone, I have a problem with the fromStringz function (std.string.fromStringz) when I try to compile with the GDC compiler (it works fine with DMD). Here is a minimal code to see the error: import std.stdio, std.string, std.c.stdlib; int main () { char* s; s = cast(char*) malloc(2); s[0] = 'a'; s[1] = '\0'; writeln(fromStringz(s)); return 0; } Compiling with DMD (works fine): $ dmd testfsz.d Compiling with GDC: $ gdc testfsz.d -o testfsz testfsz.d:8: error: undefined identifier fromStringz It does the same thing on a friend's computer. I'm using GCC 4.9.1 on Kubuntu 14.10. Any idea where this comes from? Thanks in advance for your help! fromStringz (in std.string) was introduced in D 2.066, gdc-4.9 was shipped when 2.065 was released. Iain. Thanks! I will make my own version and use conditional compilation to import it or not in that case...
fromStringz problem with gdc
Hello everyone, I have a problem with the fromStringz function (std.string.fromStringz) when I try to compile with the GDC compiler (it works fine with DMD). Here is a minimal code to see the error: import std.stdio, std.string, std.c.stdlib; int main () { char* s; s = cast(char*) malloc(2); s[0] = 'a'; s[1] = '\0'; writeln(fromStringz(s)); return 0; } Compiling with DMD (works fine): $ dmd testfsz.d Compiling with GDC: $ gdc testfsz.d -o testfsz testfsz.d:8: error: undefined identifier fromStringz It does the same thing on a friend's computer. I'm using GCC 4.9.1 on Kubuntu 14.10. Any idea where this comes from? Thanks in advance for your help!
Re: Class inside a Struct?
Thanks a lot Ali, now it works perfectly! It is quite hard to get used to D's logic, I have to stop thinking in terms of C++... Anyway, thanks again!
Class inside a Struct?
Hello everyone, I am currently learning D by coding a project, but I encountered a problem with one of my structures. I managed to reduce the code to the minimum: import std.stdio; import std.container.rbtree; struct Container { private RedBlackTree!int _rbtree = new RedBlackTree!int; void add (int elt) { _rbtree.insert(elt); } void print () { if (_rbtree.empty) { writeln(empty); } else { foreach (l; _rbtree) { write(l, ); } writeln(); } } } int main () { Container c1, c2; c1.add(1); c1.print(); c2.print(); return 0; } I don't understand why the output of this program is: 1 1 I expect it to be: 1 empty I thank you in advance for your help!