tuple-syntax

Basile B. b2.temp at gmx.com
Mon Mar 18 23:52:22 UTC 2024


On Sunday, 17 March 2024 at 20:20:52 UTC, Dom DiSc wrote:
> I just viewed the D-Conf online talk about new syntax for 
> tuples and had an idea I need to suggest:
>
> One of the proposals was to use normal round brackets. But this 
> would required to use something special to distinguish 
> one-element tuples from from a parenthesized expression. It 
> suggested a trailing comma for this case. But it has the second 
> flaw that it remains unclear how to express an empty tuple 
> unambiguously.
>
> So, how about _always_ requiring a trailing comma?
> This would make for a consistent syntax: (,) empty tuple, (x,) 
> one-element tuple, (x,y,) two element tuple ...
>
> Just an idea.

That works but I dont like that. In my opinion that would be a 
case where the syntax would serve as a hint for the semantic 
analysis. That does not break the principle of "context-free 
grammar" however; just a bit odd to me.

What about a property. Just `.tupleof` (one stone two birds 
btw...). So you put that on your one-value tuple and during sema 
it's clear what is it, let's say if it's an exp with parens. That 
works when you need an explicit type. Otherwise implicit convs 
from elem to tup and tup to elem would be enough.

```d
auto a = (0).tupleof; // infer tuple as type
static assert(isTuple, a);

immutable typeof(a) b = 0; // implicit conv, from elem to tup
immutable int c = b;       // implicit conv, from tup to elem
```


More information about the Digitalmars-d mailing list