Thread overview | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 19, 2015 let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Creating tuples and returning them from functions is trivial in D: auto getTuple() { return tuple("Bob", 42); } but using them afterwards can be confusing and error prone auto t = getTuple(); writeln("name is ", t[0], " age is ", t[1]); I really missed the ML syntax to write let (name, age) = getTuple(); Turns out this is ridiculously easy to implement in D, so here's my very tiny module for this: https://bitbucket.org/infognition/dstuff/src (scroll down to letassign.d) It allows you to write: int x, y, z, age; string name; let (name, age) = getTuple(); // tuple let (x,y,z) = argv[1..4].map!(to!int); // lazy range let (x,y,z) = [1,2,3]; // array SomeStruct s; let (s.a, s.b) = tuple(3, "piggies"); If a range or array doesn't have enough elements, this thing will throw, and if it's not desired there's let (x,y,z)[] = ... variant that uses just the available data and keeps the rest variables unchanged. |
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | On Thursday, 19 February 2015 at 04:38:32 UTC, thedeemon wrote:
> Creating tuples and returning them from functions is trivial in D:
>
> auto getTuple() { return tuple("Bob", 42); }
>
> but using them afterwards can be confusing and error prone
>
> auto t = getTuple();
> writeln("name is ", t[0], " age is ", t[1]);
>
> I really missed the ML syntax to write
>
> let (name, age) = getTuple();
>
> Turns out this is ridiculously easy to implement in D, so here's my very tiny module for this:
>
> https://bitbucket.org/infognition/dstuff/src (scroll down to letassign.d)
>
> It allows you to write:
>
> int x, y, z, age;
> string name;
>
> let (name, age) = getTuple(); // tuple
> let (x,y,z) = argv[1..4].map!(to!int); // lazy range
> let (x,y,z) = [1,2,3]; // array
>
> SomeStruct s;
> let (s.a, s.b) = tuple(3, "piggies");
>
> If a range or array doesn't have enough elements, this thing will throw, and if it's not desired there's
> let (x,y,z)[] = ...
> variant that uses just the available data and keeps the rest variables unchanged.
That's pretty neat! May I turn this code into a d-idioms? Name and link will be kept of course.
|
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | On Thursday, 19 February 2015 at 04:38:32 UTC, thedeemon wrote: > let (name, age) = getTuple(); Maybe change the name to tie: http://www.cplusplus.com/reference/tuple/tie/ ? |
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | Ola Fosheim Grøstad:
> Maybe change the name to tie:
>
> http://www.cplusplus.com/reference/tuple/tie/
>
> ?
I prefer "let", it's much more traditional and descriptive. C++ standard library is often a bad example to follow...
Bye,
bearophile
|
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | On Thursday, 19 February 2015 at 09:31:59 UTC, ponce wrote:
> That's pretty neat! May I turn this code into a d-idioms? Name and link will be kept of course.
Sure, if you wish. There was just one person using this thing until today, so I dunno whether it deserves to be in that list.
|
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | On Thursday, 19 February 2015 at 09:46:13 UTC, Ola Fosheim Grøstad wrote:
> On Thursday, 19 February 2015 at 04:38:32 UTC, thedeemon wrote:
>> let (name, age) = getTuple();
>
> Maybe change the name to tie:
> http://www.cplusplus.com/reference/tuple/tie/
> ?
SML, OCaml, Haskell, F#, ATS, Rust, Swift and others have it as "let" keyword, so personally I'd prefer continuing that tradition.
|
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 19 February 2015 at 09:50:25 UTC, bearophile wrote:
> I prefer "let", it's much more traditional and descriptive. C++ standard library is often a bad example to follow...
Doesn't "let" normally declare a new variable?
|
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | On Thursday, 19 February 2015 at 04:38:32 UTC, thedeemon wrote:
> Creating tuples and returning them from functions is trivial in D:
>
> auto getTuple() { return tuple("Bob", 42); }
>
> but using them afterwards can be confusing and error prone
>
> auto t = getTuple();
> writeln("name is ", t[0], " age is ", t[1]);
>
> I really missed the ML syntax to write
>
> let (name, age) = getTuple();
>
> Turns out this is ridiculously easy to implement in D, so here's my very tiny module for this:
>
> https://bitbucket.org/infognition/dstuff/src (scroll down to letassign.d)
>
> It allows you to write:
>
> int x, y, z, age;
> string name;
>
> let (name, age) = getTuple(); // tuple
> let (x,y,z) = argv[1..4].map!(to!int); // lazy range
> let (x,y,z) = [1,2,3]; // array
>
> SomeStruct s;
> let (s.a, s.b) = tuple(3, "piggies");
>
> If a range or array doesn't have enough elements, this thing will throw, and if it's not desired there's
> let (x,y,z)[] = ...
> variant that uses just the available data and keeps the rest variables unchanged.
that's a great example to show d's strength. thank you.
|
February 19, 2015 Re: let (x,y) = ... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Thursday, 19 February 2015 at 10:52:40 UTC, Kagamin wrote: > On Thursday, 19 February 2015 at 09:50:25 UTC, bearophile wrote: >> I prefer "let", it's much more traditional and descriptive. C++ standard library is often a bad example to follow... > > Doesn't "let" normally declare a new variable? http://ideone.com/iBzuiG - how "let" works in javascript. |
Copyright © 1999-2021 by the D Language Foundation