On Wednesday, 21 February 2018 at 06:45:14 UTC, thorstein wrote:
Hi,
I'm going circles... ;) I read a string that contains an array
of unknown dimension like:
a = [1,2,3,4] or
a = [[1,2],[3,4]] or
a = [[[1,2],[3,4]],[[5,6],[7,8]]]
With that I want to perform specified operations e.g. also
provided on command line.
Because at compile time the dimension of the array is unkown I
did not find any possibility to do something like:
auto arr = func(a);
where 'auto func(string str)' should return either a 1D, 2D, 3D
array and arr is not locked in an if(){} scope.
Is there any way and What should I'm looking for?
Thanks, thorstein
There's std.variant: https://dlang.org/phobos/std_variant
The code would probably be something like:
Algebraic!(int[], int[][], int[][][]) array;
if (value.is1D) {
array = value.read1DArray();
} else if (value.is2D) {
array = value.read2DArray();
} else {
array = value.read3DArray();
}
And you'd grab the type of value you want by array.get!(int[][]).
--
Simen