www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixed type list?

reply "seany" <seany uni-bonn.de> writes:
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?
Nov 20 2013
next sibling parent "QAston" <qaston gmail.com> writes:
On Wednesday, 20 November 2013 at 11:07:27 UTC, 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?
You can use an array of http://dlang.org/phobos/std_variant.html .
Nov 20 2013
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 20 2013
parent reply "seany" <seany uni-bonn.de> writes:
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?
Nov 20 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
seany:

 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)
cast() should be used only when you know what you are doing, and here you don't know it. I suggest to use "split" instead.
 1. either return the individual addresses of an element of an 
 array or any type of objects...
To get the address of an object just cast its pointer to something like a size_t. To get the address of array items, use &myarray[idx].
 2. generate variable names on the fly in RUNTIME?
You probably need an associative array. Bye, bearophile
Nov 20 2013