mixed type list?
seany
seany at uni-bonn.de
Wed Nov 20 07:49:12 PST 2013
On Wednesday, 20 November 2013 at 11:25:24 UTC, Jonathan M Davis
wrote:
> On Wednesday, November 20, 2013 12:07:25 seany wrote:
>> Is there any way to represent mixed data (of various types) as
>> a
>> single array?
>>
>> In scilab, we have list, and you can do list(integer, stringvar
>> ....) etc.
>>
>> Can you do something similar in D? An idea is to use a struct
>> wil all possible data types, but think that is inefficient.
>>
>> Any other ideas?
>
> That sounds more like a tuple rather than a list, as lists
> normally are
> supposed to contain values which all have the same type. So,
> the normal thing
> to do would probably be to use std.typecons.Tuple. e.g.
>
> auto t = tuple(1, 3.7, "foo");
>
> But that's definitely a tuple and not a list, so you can't
> append to it or
> remove anything from it or anything like that. If you want an
> actual list, you
> need a way to make all of the items in the list be the same
> type. And if they
> don't all share a base type (which pretty much only happens
> with classes),
> then you'd probably have to use std.variant.Variant, which is
> essentially a
> typed union. However, I'd advise against using anything like
> that unless you
> actually need it. You should favor static typing as much as
> possible rather
> than trying to hold differing types in the same list, as that's
> just begging
> for bugs, particularly when you then try and use some of those
> items as a type
> other than what they are.
>
> - Jonathan M Davis
okey, i was thinking of dynamically generating variables, and
returning their addresses to a int []. Ofcourse i first need to
cast it to int.
I have the following cases :
I have a string, this string may take the form of
a.b.c.d.e ...
The function should return
depending on another parameter,one of the following:
1. an array with [a, b, c, d, e]
2. the original string
the original string may also take the form of
(a.(b.c).(((d.e.f).(g.h) .. etc
and depending on the other parameter, we may have
1. array with [a,b,c,d, .. ]
2. array with [1, [b.c] , [[[d,e,f]],[g,h]]
...
n. the string itself
Now, i was trying the first, i used the std.algorithms.splitter,
and the result is of type *Result* and not string[], nonetheless
I can cast (how does this work? isn't typedef removed, and alias
should preserve underlaying types)
Morover while getting the addresses of the array elements, and
putting them in the integer array, all of them returns the same
address : the address of the array. (obviously). hence, i am also
wondering, is there something in D, which can
1. either return the individual addresses of an element of an
array or any type of objects...
2. generate variable names on the fly in RUNTIME?
More information about the Digitalmars-d-learn
mailing list