May 20

On Friday, 16 May 2025 at 06:08:41 UTC, Timon Gehr wrote:

>

It is not a great way to do it:

import std.typecons;

void main()@nogc{
    (int a, float[3] b, string c) = tuple(1, [0.1F, 0.2F, 0.3F], "2"); // compile error
}

The next logical step after this DIP is to enable initialization of a tuple with a struct literal:

(int a, float[3] b, string c) = { 1, [0.1F, 0.2F, 0.3F], "2" };
May 20

On Monday, 12 May 2025 at 23:01:54 UTC, Meta wrote:

>

This DIP proposes built-in tuple unpacking syntax for D. A sample of the proposed syntax:

import std.typecons : tuple;

(int a, string b) = tuple(1, "2");
assert(a == 1);
assert(b == "2");

auto (a, b) = tuple(1, "2");
static assert(is(typeof(a) == int));
static assert(is(typeof(b) == string));

auto (a, immutable b, c) = tuple(1, "2", 3.0);
static assert(is(typeof(a) == int));
static assert(is(typeof(b) == immutable string));
static assert(is(typeof(c) == double));

The DIP is based on Timon Gehr's old DIP for tuple syntax in D (https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md), but is solely limited to support for unpacking; it is not a full tuple-syntax DIP. If the reception and general sentiment for this DIP are positive, further enhancements to add built-in tuple support to D may be proposed in the future.

Thanks to Timon and Nick Trealeven for doing the bulk of the implementation and conceptual work on this proposal. I mainly just kickstarted things and am facilitating the DIP process.

The DIP:
https://github.com/MetaLang/DIPs/blob/tuple-unpacking-dip/DIPs/DIP1052.md

Let's say you have

auto (a, b) = tuple(1, "2");

and decide to change the "2" to 2, so you now have

auto (a, b) = tuple(1, 2);

And then you're, like well why do I need auto here, I know the types. So then you write

(int a, int b) = tuple(1, 2);

Ok. That's fine. But then I would wonder why this DIP doesn't allow the following syntax:

int (a, b) = tuple(1, 2);

Why would someone want this? You might say, why would someone want a tuple with all the types the same? Why not just use a static (or dynamic) array instead of a tuple? Well, if they started with a tuple, then they may not want to go back and change it. Less re-factoring. But if the unpacking syntax can work with tuples, then why not also have it work for arrays?

May 20
On 5/20/25 14:27, jmh530 wrote:
> 
> Ok. That's fine. But then I would wonder why this DIP doesn't allow the following syntax:
> ```
> int (a, b) = tuple(1, 2);
> ```

a) With C-style conventions, `int (a, b)` would say: we declare that the expression `(a, b)` has type `int`, which is not what we are looking for.

b) Grammar ambiguity. `T (a, b) = tuple(1, 2)` could just as well be read as `T(a,b).opAssign(tuple(1,2))`.
May 21

On Tuesday, 20 May 2025 at 19:29:34 UTC, Timon Gehr wrote:

>

On 5/20/25 14:27, jmh530 wrote:

>

Ok. That's fine. But then I would wonder why this DIP doesn't allow the following syntax:

int (a, b) = tuple(1, 2);

a) With C-style conventions, int (a, b) would say: we declare that the expression (a, b) has type int, which is not what we are looking for.

b) Grammar ambiguity. T (a, b) = tuple(1, 2) could just as well be read as T(a,b).opAssign(tuple(1,2)).

On a), the DMD compiler won't let you write int (a, b); It looks like what it is trying to do is to construct something (suggesting the issue is similar to what you describe in b)).

One potential solution would be that T(a, b) = tuple(1, 2) gets lowered to T(a,b).opAssign(tuple(1,2)), but T (a, b) = tuple(1, 2) gets lowered to T(a,b).opUnpack(tuple(1,2)). Potentially breaks code.

If you believe a) is an issue, does that make you cautious at all about allowing auto (a, b)? Now that I've thought about it, I wonder if people might potentially find that confusing.

1 day ago
On 5/21/25 18:34, jmh530 wrote:
> On Tuesday, 20 May 2025 at 19:29:34 UTC, Timon Gehr wrote:
>> On 5/20/25 14:27, jmh530 wrote:
>>>
>>> Ok. That's fine. But then I would wonder why this DIP doesn't allow the following syntax:
>>> ```
>>> int (a, b) = tuple(1, 2);
>>> ```
>>
>> a) With C-style conventions, `int (a, b)` would say: we declare that the expression `(a, b)` has type `int`, which is not what we are looking for.
>>
>> b) Grammar ambiguity. `T (a, b) = tuple(1, 2)` could just as well be read as `T(a,b).opAssign(tuple(1,2))`.
> 
> On a), the DMD compiler won't let you write `int (a, b);` It looks like what it is trying to do is to construct something (suggesting the issue is similar to what you describe in b)).
> 
> One potential solution would be that `T(a, b) = tuple(1, 2)` gets lowered to `T(a,b).opAssign(tuple(1,2))`, but `T (a, b) = tuple(1, 2)` gets lowered to `T(a,b).opUnpack(tuple(1,2))`. Potentially breaks code.
> ...

This is not what it would lower to, a method call cannot declare free variables in the receiver.

If you want to accept an explicit type, you have to do it in two steps:

```d
Tuple!(int, int) t = foo();
auto (a, b) = t;
```

It does not really make sense to name a type for `(a, b)`, at least not without support for tuple type syntax.

> If you believe a) is an issue, does that make you cautious at all about allowing `auto (a, b)`?

I don't see how that would be an issue for allowing `auto (a, b) =`. `auto` is not a placeholder for a type, it is a storage class, like `scope` or `immutable`.

> Now that I've thought about it, I wonder if people might potentially find that confusing.

E.g. C# supports `var (a, b) = ...;`, `(int, int) t = (1, 2);` works, `(int, int) (a, b) = (1, 2);` does not work. It's pretty much the same thing and I think it makes sense.
13 hours ago

On Tuesday, 20 May 2025 at 07:09:55 UTC, Ogion wrote:

>

The next logical step after this DIP is to enable initialization of a tuple with a struct literal:

(int a, float[3] b, string c) = { 1, [0.1F, 0.2F, 0.3F], "2" };

Firstly, from the left-hand side, that is not tuple initialization. It is unpacking into declarations. Maybe you meant to have an identifier before the = token?

Secondly, for tuple initialization from a literal, the right hand-side IMO would naturally be a tuple literal. Mentioned in (but not proposed by) the DIP:

auto t = (1, 2); // tuple literal

Also conceptually a tuple is not a struct. Unpacking a struct's fields implicitly is not supported by this DIP (and I don't think it should be as it complicates/limits future options).

12 hours ago

On Tuesday, 13 May 2025 at 03:23:14 UTC, Meta wrote:

>

On Tuesday, 13 May 2025 at 00:01:41 UTC, jmh530 wrote:

>

In your rationale, you provide an example of how it works currently. You might show how it works with the DIP as well as a contrast.

Thanks, that's a great idea.

For the record, Meta added an example with unpacking syntax for comparison:
https://github.com/MetaLang/DIPs/blob/tuple-unpacking-dip/DIPs/DIP1052.md#rationale

12 hours ago

On Monday, 2 June 2025 at 12:12:49 UTC, Nick Treleaven wrote:

>

On Tuesday, 13 May 2025 at 03:23:14 UTC, Meta wrote:

>

On Tuesday, 13 May 2025 at 00:01:41 UTC, jmh530 wrote:

>

In your rationale, you provide an example of how it works currently. You might show how it works with the DIP as well as a contrast.

Thanks, that's a great idea.

For the record, Meta added an example with unpacking syntax for comparison:
https://github.com/MetaLang/DIPs/blob/tuple-unpacking-dip/DIPs/DIP1052.md#rationale

Yeah, I had seen that. Nice improvement to the DIP.

5 hours ago

On Monday, 12 May 2025 at 23:01:54 UTC, Meta wrote:

>

This DIP proposes built-in tuple unpacking syntax for D. A sample of the proposed syntax:

import std.typecons : tuple;

[...]

Why not a full tuple DIP?

1 2 3
Next ›   Last »