improving std.array.array

monarch_dodra monarchdodra at gmail.com
Sun Jul 22 04:46:27 PDT 2012


array is one of my favorite features in Phobos. It is such a 
trivially stupid function, yet so incredibly useful/powerful. 
Just call it on any range, and you get a new raw array of the 
corresponding deferred type. Awesome!

...But I think it has one major drawback: You can't specify the 
type of the array you want. For example, if you have a range of 
ints, and you'd want to duplicate it into a range of doubles, 
well array doesn't allow that.

The problem is that:
1) It could do it no problem.
2) Array just screams to be able to write: "auto doubles = 
array!double(myInts);"

While there are... "ways"... to make it work (map with cast op) 
for example, NOTHING would be as straight forward as array!type, 
and NOTHING would be as efficient either... let alone both at the 
same time. It would allow for a powerful idiom to be even more 
powerful.

The above example is simple, but it could allow more powerful 
usage such as:
auto integralThirds = iota(1,20).map(a => a/3).array!int;
Yowza!

Other perks this allows is duplicating an immutable array into a 
non-mutable array:
"auto myMutableInts = array!int(myImmutableInts)."
or duplicating a mutable array into an imutable one.
"auto myImmutableDoubles = array!(immutable 
double)(myMutableInts)."

As this point, the current implementation does not allow either.

----
Regarding the "string versions", I propose a keeping the current 
scheme, but also give the same control of writing:
char[]  mutableShortString = array!char(someString);
wstring wideString         = array!(immutable wchar)(someString);

----
Proposing this improvement, I of course volunteer for 
implementing testing, writing unittest and pushing all this. I've 
already done most of it, and everything works like a charm ;)
These changes break nothing.

----
I discovered 2 bugs in array:
1) array always chokes at duplicating an immutable input, even 
when the input type should allow the operation. This I (think) 
can fix.
2) array(string) always returns a mutable string, since 
"ForeachType" returns a non-qualified char type. Don't know how 
to fix this :/


More information about the Digitalmars-d mailing list