www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why char[] is not nogc on this case

reply SrMordred <patric.dexheimer gmail.com> writes:
int[] a;
int[] b;

() nogc {
    foreach(v ; chain( a,b ) ) printf("%d\n", v);
}();

//Ok, everything fine;

char[] a;
char[] b;

() nogc {
    foreach(v ; chain( a,b ) ) printf("%c\n", v);
}();

//Error:  nogc delegate onlineapp.main.__lambda1 cannot call 
non- nogc function std.range.chain!(char[], 
char[]).chain.Result.front

Why?
May 24 2018
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 24 May 2018 at 23:55:24 UTC, SrMordred wrote:
 //Error:  nogc delegate onlineapp.main.__lambda1 cannot call 
 non- nogc function std.range.chain!(char[], 
 char[]).chain.Result.front

 Why?
phobos automatically decodes utf8 into dchars. This can throw a new utf exception.
May 24 2018
parent reply SrMordred <patric.dexheimer gmail.com> writes:
On Friday, 25 May 2018 at 00:04:10 UTC, Adam D. Ruppe wrote:
 On Thursday, 24 May 2018 at 23:55:24 UTC, SrMordred wrote:
 //Error:  nogc delegate onlineapp.main.__lambda1 cannot call 
 non- nogc function std.range.chain!(char[], 
 char[]).chain.Result.front

 Why?
phobos automatically decodes utf8 into dchars. This can throw a new utf exception.
Oh its the utf8 decode thing. I forgot about it. I thought that it only decodes when using strings and dchars. Thanks!
May 24 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, May 25, 2018 00:09:28 SrMordred via Digitalmars-d-learn wrote:
 On Friday, 25 May 2018 at 00:04:10 UTC, Adam D. Ruppe wrote:
 On Thursday, 24 May 2018 at 23:55:24 UTC, SrMordred wrote:
 //Error:  nogc delegate onlineapp.main.__lambda1 cannot call
 non- nogc function std.range.chain!(char[],
 char[]).chain.Result.front

 Why?
phobos automatically decodes utf8 into dchars. This can throw a new utf exception.
Oh its the utf8 decode thing. I forgot about it. I thought that it only decodes when using strings and dchars.
It happens with any "narrow string" - i.e. any array of char or dchar. You can test for it with std.traits.isNarrowString, whose current implementation is: enum bool isNarrowString(T) = isSomeString!T && !is(T : const dchar[]); Basically, unless a range-based function goes out of its way to specialize for narrow strings and avoids any decoding (and whether that makes any sense depends on what the function does), it's going to treat any array of char or wchar as a range of dchar and will throw a UTFException on invalid Unicode. - Jonathan M Davis
May 24 2018
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, May 24, 2018 23:55:24 SrMordred via Digitalmars-d-learn wrote:
 int[] a;
 int[] b;

 () nogc {
     foreach(v ; chain( a,b ) ) printf("%d\n", v);
 }();

 //Ok, everything fine;

 char[] a;
 char[] b;

 () nogc {
     foreach(v ; chain( a,b ) ) printf("%c\n", v);
 }();

 //Error:  nogc delegate onlineapp.main.__lambda1 cannot call
 non- nogc function std.range.chain!(char[],
 char[]).chain.Result.front

 Why?
Because arrays of char and wchar are treated as ranges of dchar. std.primitives.range.front and popFront call std.utf.decode and stride respectively so that front returns dchar. decode (and possibly stride - I'd have to check) throw a UTFException on invalid Unicode, and that means that they allocate with the GC. If you want to avoid the auto-decoding, you have to use something like std.string.representation or std.utf.byCodeUnit to wrap the array of chars before passing them to any range-based functions. - Jonathan M Davis
May 24 2018
parent SrMordred <patric.dexheimer gmail.com> writes:
 Because arrays of char and wchar are treated as ranges of dchar.
That part that I didnt know, thanks! :)
May 24 2018