Thread overview | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 30, 2013 A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Regarding the syntax to unpack tuples into single variables, Kenji Hara wrote a DIP (http://wiki.dlang.org/DIP32 ) denoting tuples with the univesal syntax {...}, but people have found some problems in it. (I think Kenji didn't update that DIP with all the small improvements we suggested in that thread, so they risk getting lost.) Maybe one solution is to use a "tup(...)" syntax, it's a bit heavy, but it's clear and maybe it has no corner cases: tup(int, string) tup = tup(1, "hi"); foreach (Float; tup(float, double, real)) { ... } auto tup(x, y) = tup(1, "hi"); tup(auto x, y) = tup(1, "hi"); tup(int x, string y) = tup(1, "hi"); foreach (i, const tup(x, y); [tup(1,2), tup(3,4), tup(5,6), ...]) { void foo(tup(int, string name), string msg); (tup(A a, B b)) => a + b; switch (tup) { case tup(1, 2): ... } This is not very elegant, "tup" becomes a new keyword and generally I don't like such strong abbreviations, like the ones used in Rust language, but "tuple()" clashes with the library-defined ones, and it's even more wordy if you want to define an array of them: [tuple(1,2), tuple(3,4), tuple(5,6), ...] Bye, bearophile |
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | If you don't like to introduce a new keyword, then a different alternative is to use the @ again, a bit like UDAs: @{...} @[10] @{int, string} tup = @{1, "hi"}; // With UDA. foreach (Float; @{float, double, real}) { ... } auto @{x, y} = @{1, "hi"}; @{auto x, y} = @{1, "hi"}; @{int x, string y} = @{1, "hi"}; foreach (i, const @{x, y}; [@{1,2}, @{3,4}, @{5,6}, ...]) { void foo(@{int, string name}, string msg); (@{A a, B b}) => a + b; switch (tup) { case @{1, 2}: ... } Bye, bearophile |
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 30 May 2013 at 12:32:46 UTC, bearophile wrote:
> If you don't like to introduce a new keyword, then a different alternative is to use the @ again, a bit like UDAs:
>
> @{...}
>
> @[10] @{int, string} tup = @{1, "hi"}; // With UDA.
> foreach (Float; @{float, double, real}) { ... }
> auto @{x, y} = @{1, "hi"};
> @{auto x, y} = @{1, "hi"};
> @{int x, string y} = @{1, "hi"};
> foreach (i, const @{x, y}; [@{1,2}, @{3,4}, @{5,6}, ...]) {
> void foo(@{int, string name}, string msg);
> (@{A a, B b}) => a + b;
> switch (tup) { case @{1, 2}: ... }
>
> Bye,
> bearophile
My eyes, they burn...
|
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Fuente | Frank Fuente:
> My eyes, they burn...
It looks a bit like Perl, it's not very nice, but for me it's not easy to find something significantly better.
The syntax with tup() is longer but less noisy.
Bye,
bearophile
|
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 30 May 2013 at 13:02:24 UTC, bearophile wrote:
> Frank Fuente:
> The syntax with tup() is longer but less noisy.
I prefer tup() over the @{} thing, although I would like to have something that looks like "a syntax" instead of "a function".
|
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Am 30.05.2013 15:02, schrieb bearophile:
> Frank Fuente:
>
>> My eyes, they burn...
>
> It looks a bit like Perl, it's not very nice, but for me it's not
> easy to find something significantly better.
> The syntax with tup() is longer but less noisy.
>
> Bye,
> bearophile
>
what about !(float, double, real) because the tupple syntax is near to template parameters
|
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | Am 30.05.2013 15:43, schrieb dennis luehring:
> Am 30.05.2013 15:02, schrieb bearophile:
>> Frank Fuente:
>>
>>> My eyes, they burn...
>>
>> It looks a bit like Perl, it's not very nice, but for me it's not
>> easy to find something significantly better.
>> The syntax with tup() is longer but less noisy.
>>
>> Bye,
>> bearophile
>>
>
> what about !(float, double, real) because the tupple syntax is near to
> template parameters
>
sadly not context free and indentical strange like !{float, double, real}
|
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | Could also do something in the style of token strings, ie. t{ ... } It's lighter than "tup" and there's a precedent for it already in the language with q{ ... } which also means there should be no issues parsing it. |
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Diggory | On Thursday, 30 May 2013 at 14:06:15 UTC, Diggory wrote:
> Could also do something in the style of token strings, ie.
>
> t{ ... }
>
> It's lighter than "tup" and there's a precedent for it already in the language with q{ ... } which also means there should be no issues parsing it.
These are the hacks that bring languages into a kind of brainfuck syntax. Why not keep a language easy readable and understandable. A precedence does not imply that it is a desirable thing to have.
|
May 30, 2013 Re: A different tuple syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to watcher | Am 30.05.2013 16:26, schrieb watcher:
> On Thursday, 30 May 2013 at 14:06:15 UTC, Diggory wrote:
>> Could also do something in the style of token strings, ie.
>>
>> t{ ... }
>>
>> It's lighter than "tup" and there's a precedent for it already
>> in the language with q{ ... } which also means there should be
>> no issues parsing it.
>
> These are the hacks that bring languages into a kind of brainfuck
> syntax. Why not keep a language easy readable and understandable.
> A precedence does not imply that it is a desirable thing to have.
>
then is the only solution to free tuple/or use tup with {}
this way a tuple is similar from a syntax perspective like class or struct - the {} scope defines the interface of the tuple which are by default unnamed ordered aliases for type/value
class { int a; int b; ... }
tuple { int, double, "hello" }
its easy to parse, context free and doesn't need a new type of scope
|
Copyright © 1999-2021 by the D Language Foundation