Array of structs construction

timotheecour thelastmammoth at gmail.com
Mon Oct 22 21:51:00 PDT 2012


Can't we simply use map, so as not to introduce new syntax?

import std.algorithm:map;
import std.array:array;

// some imports here
void main() {
     //BigInt[] data1 = [5, 6, 9];
     auto data1 = [5, 6, 9].map!(a=>BigInt(a)).array;
     Ranged!(int,5,10)[] data2 = [5, 6, 9];
     auto data2 = [5, 6, 9].map!(a=>Ranged!(int,5,10)).array;
}

I think this is less verbose than something like:
AStruct[] arr = mixin(structArray!AStruct("1,1", "1,2", "2,2", 
"2,3"));

Also, we can make it even more concise by:
auto data1 = [5, 6, 9].map!(make!BigInt).array; //make has been 
proposed as a proxy to construct a struct/class

or even the very concise:
auto data1 = [5, 6, 9].mapMake!BigInt;

where mapMake would be similar to std.algorithm.map except it 
would take a struct (or class) type instead of a delegate. It's 
code should be fairly obvious. We could choose whether to return 
a lazy range or not in that case so the ".array" is not required.




More information about the Digitalmars-d mailing list