Array of structs construction

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 19 22:32:48 PDT 2012


On Sat, Oct 20, 2012 at 03:06:56AM +0200, bearophile wrote:
> I'd like to write code like this (the constructors of the structs
> must take 1 argument):
> 
> // some imports here
> void main() {
>     BigInt[] data1 = [5, 6, 9];
>     Ranged!(int,5,10)[] data2 = [5, 6, 9];
>     Nibble[] data3 = [1, 2, 15]; // Nibble.sizeof == 1
>     alias Typedef!int Mint;
>     Mint[] data4 = [5, 6, 9];
> }
> 
> 
> Do you like this feature?
[...]

What about mixin() with a CTFE function that expands an array of
initializers at compile-time? Something like:

	string structArray(S,T...)(T args)
		if (is(typeof(S(args[0]))))
	{
		// Warning: untested code
		return "[" ~ join(
			map!(a) => S.stringof ~ "(" ~ a ~ ")", ",") ~
			"]";
	}

	S[] sArr = mixin(structArray(elem0, elem1, elem2));


T

-- 
English has the lovely word "defenestrate", meaning "to execute by
throwing someone out a window", or more recently "to remove Windows from
a computer and replace it with something useful". :-) -- John Cowan


More information about the Digitalmars-d mailing list