digitalmars.D.learn - return type based on content of an array
- thorstein (15/15) Feb 20 2018 Hi,
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (14/29) Feb 21 2018 There's std.variant: https://dlang.org/phobos/std_variant
- Steven Schveighoffer (18/39) Feb 21 2018 If you want a specific type that the type system cares about, you have
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
Feb 20 2018
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
Feb 21 2018
On 2/21/18 1:45 AM, 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?
If you want a specific type that the type system cares about, you have
to implement every possibility. Then you can use Variant.
But if you want a recursive strategy, where you need the dimensions at
runtime only, then you can write a struct with a tagged union which
gives you all the info you need:
struct NDimArray
{
size_t dimensions; // if 0, then this is a value
union {
NDimArray[] elements;
int value;
}
}
You can probably figure out the rest. Probably you can eliminate the
dimensions field somehow to save space.
This is how things like JSON DOM structures work.
-Steve
Feb 21 2018









Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> 