Re: let (x,y) = ...
On Tuesday, 24 November 2015 at 05:45:55 UTC, thedeemon wrote: Well, I believe it's a matter of taste. By allowing different number of elements there you allow more errors to sink in without gaining anything at all. You lose the choice between strict and loose operators, erase the difference. It's not the "consistency" I would like to have. ok, always curious about strategic choices, thanks :-)
Re: let (x,y) = ...
On Monday, 23 November 2015 at 22:32:57 UTC, visitor wrote: On Monday, 23 November 2015 at 20:10:49 UTC, visitor wrote: Andrea Fontana(s allows let (hello, world) = ["hi", "there", "!"]; of course in your version let (hello, world)[] = ["hi", "there", "!"] works but for consistency with range, i think Fontana's note is relevant Well, I believe it's a matter of taste. By allowing different number of elements there you allow more errors to sink in without gaining anything at all. You lose the choice between strict and loose operators, erase the difference. It's not the "consistency" I would like to have.
Re: let (x,y) = ...
On Monday, 23 November 2015 at 20:10:49 UTC, visitor wrote: Andrea Fontana(s allows let (hello, world) = ["hi", "there", "!"]; of course in your version let (hello, world)[] = ["hi", "there", "!"] works but for consistency with range, i think Fontana's note is relevant
Re: let (x,y) = ...
On Monday, 23 November 2015 at 18:38:45 UTC, thedeemon wrote: let (hello, world)[] = arr; i think what Andrea Fontana is talking is the other way around your solution allows let (hello, world)[] = ["hi"]; Andrea Fontana(s allows let (hello, world) = ["hi", "there", "!"];
Re: let (x,y) = ...
On Monday, 23 November 2015 at 16:58:43 UTC, Andrea Fontana wrote: Nice. Why first enforce is "==" rather than ">=" ? This prevents something like: auto arr = ["hello", "world", "!"]; let (hello, world) = arr; The very first post of this thread should have answered this. Two options are available: one requires exact number of elements and so catches more errors, the other requires there to be "enough" data, for cases where you want that. To get behavior you described just use let (hello, world)[] = arr;
Re: let (x,y) = ...
On Monday, 23 November 2015 at 16:58:43 UTC, Andrea Fontana wrote: Nice. Why first enforce is "==" rather than ">=" ? This prevents something like: auto arr = ["hello", "world", "!"]; string hello; string world; let (hello, world) = arr; note that this is thedeemon's work ! (sorry couldn't resist) anyway, yes indeed !
Re: let (x,y) = ...
On Monday, 23 November 2015 at 11:12:33 UTC, visitor wrote: this work fine with your unittest : auto let(Ts...)(ref Ts vars) { struct Let { void opAssign( Tuple!Ts xs ) { foreach(i, t; Ts) vars[i] = xs[i]; } static if (sameTypes!Ts) { import std.conv : text; void opAssign(Ts[0][] xs) { // redundant but more effective enforce(xs.length == Ts.length, "let (...) = ...: array must have " ~ Ts.length.text ~ " elements."); foreach(i, t; Ts) vars[i] = xs[i]; } void opAssign(R)(R xs) if (isInputRange!R && is(ElementType!R == Ts[0])) { static if (hasLength!R) { enforce(xs.length >= Ts.length, "let (...) = ...: range must have at least " ~ Ts.length.text ~ " elements."); } foreach(i, t; Ts) { enforce(!xs.empty, "let (...) = ...: range must have at least " ~ Ts.length.text ~ " elements."); vars[i] = xs.front; xs.popFront(); } } void opIndexAssign(R)(R xs) if (isInputRange!R && is(ElementType!R == Ts[0])) { foreach(i, t; Ts) { if(xs.empty) return; vars[i] = xs.front; xs.popFront(); } } } } return Let(); } Nice. Why first enforce is "==" rather than ">=" ? This prevents something like: auto arr = ["hello", "world", "!"]; string hello; string world; let (hello, world) = arr;
Re: let (x,y) = ...
On Monday, 23 November 2015 at 14:54:15 UTC, thedeemon wrote: Yep, this way it works too, by capturing input vars in a closure. So the main difference is that your variant allocates GC memory while original variant does not allocate anything in the heap (only on stack). Thanks for clarifying :-) hope this will end into the language ! great work.
Re: let (x,y) = ...
On Monday, 23 November 2015 at 11:12:33 UTC, visitor wrote: My original solution remembers in the constructor addresses of variables to fill, then does the filling in opAssign operator, so I needed a way to store the references and used pointers for that. yes, but you are using ref : "auto let(Ts...)(ref Ts vars)" so vars are changed, no need to store anything, no? i was wondering if there is some subtleties or efficiency reasons for using pointers Thanks for the code! Yep, this way it works too, by capturing input vars in a closure. So the main difference is that your variant allocates GC memory while original variant does not allocate anything in the heap (only on stack).
Re: let (x,y) = ...
On Monday, 23 November 2015 at 10:28:53 UTC, thedeemon wrote: On Sunday, 22 November 2015 at 18:47:34 UTC, visitor wrote: What is the reason for using pointers (alias pointerOf(T) = T* etc...) it works without ! what am i missing ? What and how exactly works without? My original solution remembers in the constructor addresses of variables to fill, then does the filling in opAssign operator, so I needed a way to store the references and used pointers for that. yes, but you are using ref : "auto let(Ts...)(ref Ts vars)" so vars are changed, no need to store anything, no? i was wondering if there is some subtleties or efficiency reasons for using pointers this work fine with your unittest : auto let(Ts...)(ref Ts vars) { struct Let { void opAssign( Tuple!Ts xs ) { foreach(i, t; Ts) vars[i] = xs[i]; } static if (sameTypes!Ts) { import std.conv : text; void opAssign(Ts[0][] xs) { // redundant but more effective enforce(xs.length == Ts.length, "let (...) = ...: array must have " ~ Ts.length.text ~ " elements."); foreach(i, t; Ts) vars[i] = xs[i]; } void opAssign(R)(R xs) if (isInputRange!R && is(ElementType!R == Ts[0])) { static if (hasLength!R) { enforce(xs.length >= Ts.length, "let (...) = ...: range must have at least " ~ Ts.length.text ~ " elements."); } foreach(i, t; Ts) { enforce(!xs.empty, "let (...) = ...: range must have at least " ~ Ts.length.text ~ " elements."); vars[i] = xs.front; xs.popFront(); } } void opIndexAssign(R)(R xs) if (isInputRange!R && is(ElementType!R == Ts[0])) { foreach(i, t; Ts) { if(xs.empty) return; vars[i] = xs.front; xs.popFront(); } } } } return Let(); }
Re: let (x,y) = ...
On Sunday, 22 November 2015 at 18:47:34 UTC, visitor wrote: What is the reason for using pointers (alias pointerOf(T) = T* etc...) it works without ! what am i missing ? What and how exactly works without? My original solution remembers in the constructor addresses of variables to fill, then does the filling in opAssign operator, so I needed a way to store the references and used pointers for that.
Re: let (x,y) = ...
On Friday, 20 February 2015 at 09:12:26 UTC, Jacob Carlborg wrote: On 2015-02-19 05:38, 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(); Didn't someone create a pull request for something like: auto(name, age) = getTuple(); Or was it a DIP? Waw! auto(name, age) = getTuple(); looks better :)
Re: let (x,y) = ...
hello, Learning here, hope i don"t excavate unnecessarily an old post What is the reason for using pointers (alias pointerOf(T) = T* etc...) it works without ! what am i missing ? Thanks
Re: let (x,y) = ...
Nick Treleaven, el 19 de February a las 17:25 me escribiste: > On 19/02/2015 17:00, Nick Treleaven wrote: > >>>Alternatively std.typetuple.TypeTuple can be used instead of let > >> > >>not for ranges and arrays though > > > >Yes, but `tuple` overloads could be added for those. > > Or not - the length isn't known at compile-time. > > >Tuple already > >supports construction from a static array: > > > > int a, b; > > TypeTuple!(a, b) = Tuple!(int, int)([3, 4]); > > I'm hacking std.typecons so this does work: > > TypeTuple!(a, b) = [4, 5].tuple; Why not to integrate this "let" to phobos, that seems to be a lot of syntactic noise compared to : let (a, b) = [4, 5]; -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ -- You can try the best you can If you try the best you can The best you can is good enough
Re: let (x,y) = ...
On Friday, 20 February 2015 at 09:12:26 UTC, Jacob Carlborg wrote: On 2015-02-19 05:38, 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(); Didn't someone create a pull request for something like: auto(name, age) = getTuple(); Or was it a DIP? This one, by Kenji? http://wiki.dlang.org/DIP32
Re: let (x,y) = ...
On 2015-02-19 05:38, 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(); Didn't someone create a pull request for something like: auto(name, age) = getTuple(); Or was it a DIP? -- /Jacob Carlborg
Re: let (x,y) = ...
On 19/02/2015 17:00, Nick Treleaven wrote: Alternatively std.typetuple.TypeTuple can be used instead of let not for ranges and arrays though Yes, but `tuple` overloads could be added for those. Or not - the length isn't known at compile-time. Tuple already supports construction from a static array: int a, b; TypeTuple!(a, b) = Tuple!(int, int)([3, 4]); I'm hacking std.typecons so this does work: TypeTuple!(a, b) = [4, 5].tuple;
Re: let (x,y) = ...
On 19/02/2015 14:59, John Colvin wrote: On Thursday, 19 February 2015 at 13:52:29 UTC, Nick Treleaven wrote: On 19/02/2015 04:38, thedeemon wrote: 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"); Alternatively std.typetuple.TypeTuple can be used instead of let not for ranges and arrays though Yes, but `tuple` overloads could be added for those. Tuple already supports construction from a static array: int a, b; TypeTuple!(a, b) = Tuple!(int, int)([3, 4]);
Re: let (x,y) = ...
"let" reads better either way I think. "let this and that equal this other thing". On Thu, Feb 19, 2015 at 2:00 PM, bearophile via Digitalmars-d-announce < digitalmars-d-announce@puremagic.com> wrote: > Kagamin: > > Doesn't "let" normally declare a new variable? >> > > You are right, yours is a valid point... So "tie" could be a better name > after all. > > Bye, > bearophile >
Re: let (x,y) = ...
On Thursday, 19 February 2015 at 13:52:29 UTC, Nick Treleaven wrote: On 19/02/2015 04:38, thedeemon wrote: 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"); Alternatively std.typetuple.TypeTuple can be used instead of let not for ranges and arrays though
Re: let (x,y) = ...
On 19/02/2015 04:38, thedeemon wrote: 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"); Alternatively std.typetuple.TypeTuple can be used instead of let: http://forum.dlang.org/post/op.wa4vn6lgsqugbd@localhost 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. With these functions you can skip certain elements: http://forum.dlang.org/post/jjnmh2$27o5$1...@digitalmars.com
Re: let (x,y) = ...
Kagamin: Or even more obvious (VBA,TSQL): set (x,y,z) = [1,2,3]; I prefer to use "set" as in Python, to define sets: s = set([1, 2, 3]) 2 in s True Bye, bearophile
Re: let (x,y) = ...
On 02/19/2015 11:04 AM, thedeemon wrote: SML, OCaml, Haskell, F#, ATS, Rust, Swift and others have it as "let" keyword, so personally I'd prefer continuing that tradition. It's semantically different though because it doesn't declare the variables.
Re: let (x,y) = ...
On 02/19/2015 12:59 PM, bearophile wrote: It's also a great way to show what's missing in D syntax. True that.
Re: let (x,y) = ...
Or even more obvious (VBA,TSQL): set (x,y,z) = [1,2,3];
Re: let (x,y) = ...
Kagamin: Doesn't "let" normally declare a new variable? You are right, yours is a valid point... So "tie" could be a better name after all. Bye, bearophile
Re: let (x,y) = ...
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.
Re: let (x,y) = ...
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
Re: let (x,y) = ...
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.
Re: let (x,y) = ...
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?
Re: let (x,y) = ...
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.
Re: let (x,y) = ...
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.
Re: let (x,y) = ...
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
Re: let (x,y) = ...
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/ ?
Re: let (x,y) = ...
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.
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.