digitalmars.D.learn - Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays.
- John Carter (14/14) Jun 23 2014 I guess between perl and Ruby and Scheme etc. I got used to
- Chris Williams (6/20) Jun 23 2014 More likely what you want are variants:
- John Carter (11/12) Jun 23 2014 Hmm. Interesting.
- Chris Williams (3/15) Jun 25 2014 D has structs. If you have any problem working with Tuples, you
- Ary Borenszweig (16/30) Jun 23 2014 Union types are very common (I use them every day), and IMHO it's very
- bearophile (4/8) Jun 23 2014 Take a look at Algebraic in Phobos.
- John Carter (3/8) Jun 23 2014 Hmm. egrepping through /usr/include/dmd fails to find
- Meta (32/41) Jun 23 2014 You're looking for std.variant.Algebraic and std.typecons.Tuple.
I guess between perl and Ruby and Scheme etc. I got used to creating hybrid containers.... Want a pair of [string, fileList]? Just make an Array with two items, one a string, one and array of strings. Done. D barfed... leaving me momentarily stunned... then Oh Yes, type safety, Tuple's are the answer where Tuples where Tuples... Eventually found http://dlang.org/tuple.html and more specifically the somewhat unexpectedly named http://dlang.org/phobos/std_typecons.html and off I went... I do have a personal design guideline of when you adding too much behaviour to a heterocontainer, refactor into a class. But I guess I have never realised how often I do casually create heterogenous containers.... Just rambling and musing.
Jun 23 2014
On Monday, 23 June 2014 at 21:18:39 UTC, John Carter wrote:I guess between perl and Ruby and Scheme etc. I got used to creating hybrid containers.... Want a pair of [string, fileList]? Just make an Array with two items, one a string, one and array of strings. Done. D barfed... leaving me momentarily stunned... then Oh Yes, type safety, Tuple's are the answer where Tuples where Tuples... Eventually found http://dlang.org/tuple.html and more specifically the somewhat unexpectedly named http://dlang.org/phobos/std_typecons.html and off I went... I do have a personal design guideline of when you adding too much behaviour to a heterocontainer, refactor into a class. But I guess I have never realised how often I do casually create heterogenous containers.... Just rambling and musing.More likely what you want are variants: http://dlang.org/library/std/variant/variantArray.html http://dlang.org/library/std/variant.html Tuples can hold multiple types, but they're only available during compile time.
Jun 23 2014
On Monday, 23 June 2014 at 21:26:19 UTC, Chris Williams wrote:More likely what you want are variants:Hmm. Interesting. Yes, Variant and VariantArray are much closer to the dynamic language semantics... But the interesting thing is Tuple is much closer to "What I Mean" when I create these things. I probably should used Rubies http://ruby-doc.org/core-2.0/Struct.html but apart from naming the elements it gives me nothing beyond more keystrokes to enter. Tuple is very similar, but also grants type safety.
Jun 23 2014
On Monday, 23 June 2014 at 22:08:59 UTC, John Carter wrote:On Monday, 23 June 2014 at 21:26:19 UTC, Chris Williams wrote:D has structs. If you have any problem working with Tuples, you might consider moving over.More likely what you want are variants:Hmm. Interesting. Yes, Variant and VariantArray are much closer to the dynamic language semantics... But the interesting thing is Tuple is much closer to "What I Mean" when I create these things. I probably should used Rubies http://ruby-doc.org/core-2.0/Struct.html but apart from naming the elements it gives me nothing beyond more keystrokes to enter. Tuple is very similar, but also grants type safety.
Jun 25 2014
On 6/23/14, 6:18 PM, John Carter wrote:I guess between perl and Ruby and Scheme etc. I got used to creating hybrid containers.... Want a pair of [string, fileList]? Just make an Array with two items, one a string, one and array of strings. Done. D barfed... leaving me momentarily stunned... then Oh Yes, type safety, Tuple's are the answer where Tuples where Tuples... Eventually found http://dlang.org/tuple.html and more specifically the somewhat unexpectedly named http://dlang.org/phobos/std_typecons.html and off I went... I do have a personal design guideline of when you adding too much behaviour to a heterocontainer, refactor into a class. But I guess I have never realised how often I do casually create heterogenous containers.... Just rambling and musing.Union types are very common (I use them every day), and IMHO it's very nice to have them included in the language (either built-in or as a library solution). As a library solution I would do something like this: Union!(int, string)[] elements; elements ~= 1; elements ~= "hello"; auto x = elements[0].cast!(int); // etc. The difference with Variant is that (I think) Variant allows any kind of type to be inserted into it, but for a Union you are just limited to a set of types. For example, the following would be a compile-time error: elements[0].cast!(float); // error, Union!(int, string) doesn't include float As a built-in way the way to use it would be much nicer, but of course it involves too many changes for that to happen...
Jun 23 2014
Ary Borenszweig:As a library solution I would do something like this: Union!(int, string)[] elements; elements ~= 1; elements ~= "hello";Take a look at Algebraic in Phobos. Bye, bearophile
Jun 23 2014
On Monday, 23 June 2014 at 21:49:29 UTC, Ary Borenszweig wrote:Union types are very common (I use them every day), and IMHO it's very nice to have them included in the language (either built-in or as a library solution). As a library solution I would do something like this: Union!(int, string)[] elements;Hmm. egrepping through /usr/include/dmd fails to find '\bUnion\b', are you sure you don't mean Algebraic?
Jun 23 2014
On Monday, 23 June 2014 at 22:11:57 UTC, John Carter wrote:On Monday, 23 June 2014 at 21:49:29 UTC, Ary Borenszweig wrote:You're looking for std.variant.Algebraic and std.typecons.Tuple. Tuple is actually a library-defined struct, with no compiler magic. The tuple situation in D is a bit weird; there are compiler tuples (which you don't need to worry about in this case), and library tuples, i.e., std.typecons.Tuple. There's also at least 1 other kind of special tuple, but you should hardly ever need to deal with that. For most cases, just use std.typecons.Tuple. Here's a few simple examples: import std.typecons: Tuple; Tuple!(string, File[]) getDirListing(string dir) { //... } //Even better alias DirListing = Tuple!(string, File[]); DirListing getDirListing(string dir) { //... } //Adding a string after a tuple member names that field alias DirListing = Tuple!(string, "dir", File[], "files"); void main() { //These functions for operating on files are just made up enum directory = "C:\SomeDir"; auto dirListing = getDirListing(directory); foreach (i, file; dirListing.files) { file.setName("%s-%s".format(dirListing.name, i); } }Union types are very common (I use them every day), and IMHO it's very nice to have them included in the language (either built-in or as a library solution). As a library solution I would do something like this: Union!(int, string)[] elements;Hmm. egrepping through /usr/include/dmd fails to find '\bUnion\b', are you sure you don't mean Algebraic?
Jun 23 2014