Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
June 23, 2014 Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
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. |
June 23, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carter | 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. |
June 23, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carter | 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...
|
June 23, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | 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
|
June 23, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Williams | 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. |
June 23, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | 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?
|
June 24, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carter | 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: > >> 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? 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); } } |
June 26, 2014 Re: Momentary Eh?! for a Dynamic Language Programmmer. Tuples vs Arrays. Just rambling. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carter | 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:
>
>> 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.
D has structs. If you have any problem working with Tuples, you might consider moving over.
|
Copyright © 1999-2021 by the D Language Foundation