== Quote from Andrei Alexandrescu ([email protected])'s article > On 09/12/2010 07:09 PM, bearophile wrote: > > Andrei Alexandrescu: > >> This goes into "bearophile's odd posts coming now and then". > > > > I assume you have missed most of the things I was trying to say, maybe you have not even read the original post. So I try to explain better a subset of the things I have written. > > > > This is a quite common piece of Python code: > > > > from random import sample > > d = "0123456789" > > print "".join(sample(d, 2)) > Well it's not that common code. How often would one need to generate a > string that contains two random but distinct digits? > > I need to perform the same thing in D. > > For me it's not easy to do that in D2 with Phobos2. > > > > This doesn't work: > > > > import std.stdio, std.random, std.array, std.range; > > void main() { > > string d = "0123456789"; > > string res = array(take(randomCover(d, rndGen), 2)); > > writeln(res); > > } > > > > It returns: > > test.d(4): Error: cannot implicitly convert expression (array(take(randomCover(d,rndGen()),2u))) of type dchar[] to string > The code compiles and runs as written on my system. I think it's David > Simcha who changed the return type to ForEachType!Range[]. I'm not sure > I agree with that, as it takes an oddity of foreach that I hoped would > go away some time and propagates it.
Just to clear up some confusion, I specialized array() for narrow strings so it always returns a dchar[] instead of using ForeachType. Therefore, the behavior is effectively the same as before I changed array() to work with opApply, when it used ElementType. I figured there's two use cases for calling array() on a narrow string: Generic code and non-generic code. In generic code you want to be able to assume that the array returned will be a random access range with lvalue elements like every array type besides narrow strings is. In non-generic code you can just use std.conv to get exactly the type you want.
