Array of structs construction

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


On Fri, Oct 19, 2012 at 10:42:08PM -0700, H. S. Teoh wrote:
[...]
> 	import std.stdio;
> 	string structArray(S)(string[] args...) {
> 		string s = "[";
> 		string delim = "";
> 		foreach (a; args) {
> 			s ~= delim ~ S.stringof ~ "(" ~ a ~ ")";
> 			delim = ",";
> 		}
> 		return s ~ "]";
> 	}
[...]

Here's how to use it for making BigInt arrays:

	void main() {
		BigInt[] arr = mixin(structArray!BigInt(
			`"12345678901234567890"`,
			`"4294967296000"`,
			`"1000000000000000000000000000000000"`,
			`"1203004000500006000007000000800000009"`,
		));

		writeln(arr);
	}

It works for any kind of struct with ctors. Should be trivially
extendible to array of class objects. Some improvement in usage syntax
is probably possible, too, but I'll leave it up to the CTFE experts here
to figure that out. ;-)


T

-- 
Why ask rhetorical questions? -- JC


More information about the Digitalmars-d mailing list