Jump to page: 1 24  
Page
Thread overview
let (x,y) = ...
Feb 19, 2015
thedeemon
Feb 19, 2015
ponce
Feb 19, 2015
thedeemon
Feb 19, 2015
bearophile
Feb 19, 2015
Kagamin
Feb 19, 2015
Kagamin
Feb 19, 2015
bearophile
Feb 19, 2015
Kagamin
Feb 19, 2015
bearophile
Feb 19, 2015
Rory McGuire
Feb 19, 2015
thedeemon
Feb 19, 2015
Martin Nowak
Feb 19, 2015
Mengu
Feb 19, 2015
bearophile
Feb 19, 2015
Martin Nowak
Feb 19, 2015
Nick Treleaven
Feb 19, 2015
John Colvin
Feb 19, 2015
Nick Treleaven
Feb 19, 2015
Nick Treleaven
Feb 24, 2015
Leandro Lucarella
Feb 20, 2015
Jacob Carlborg
Feb 20, 2015
Marc Schütz
Nov 23, 2015
karabuta
Nov 22, 2015
visitor
Nov 23, 2015
thedeemon
Nov 23, 2015
visitor
Nov 23, 2015
thedeemon
Nov 23, 2015
visitor
Nov 23, 2015
Andrea Fontana
Nov 23, 2015
visitor
Nov 23, 2015
thedeemon
Nov 23, 2015
visitor
Nov 23, 2015
visitor
Nov 24, 2015
thedeemon
Nov 24, 2015
visitor
February 19, 2015
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
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
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
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
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
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
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
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
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.
February 19, 2015
Mengu:

> that's a great example to show d's strength. thank you.

It's also a great way to show what's missing in D syntax.

Bye,
bearophile
« First   ‹ Prev
1 2 3 4