www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Strings iteration and immutable

This comes from a discussion in this issue:
https://issues.dlang.org/show_bug.cgi?id=13122

Perhaps this topic was already discussed and solved in past, but 
unfortunately I don't remember such discussions.

Issue 13122 is about cartesianProduct but the same situation is 
visible more simply with std.algorithm.filter:

void main() {
     import std.algorithm: filter;
     immutable a = "ABC";
     foreach (t1; a.filter!(x => true))
         pragma(msg, typeof(t1));
     immutable b = "ABC"d;
     foreach (t2; b.filter!(x => true))
         pragma(msg, typeof(t2));
}


Output:

dchar
immutable(dchar)


So cartesianProduct seems to be working like the other Phobos 
functions and I'll have to close Issue 13122 as worksforme.


a.filter yields dchar because range iteration converts ranges of 
chars to ranges of dchars. But then why isn't the output like 
this?

immutable(dchar)
immutable(dchar)

The string a is composed of immutable(char) after all.

For me it's strange that iterating on arrays of immutable chars 
or immutable dchars gives an output of different "constness". 
This looks like a gratuitous breakage of symmetry :-)

Bye,
bearophile
Sep 21 2014