Jump to page: 1 2
Thread overview
Tuple Unpacking Syntax
4 days ago
Meta
4 days ago
Meta
4 days ago
Adam D. Ruppe
4 days ago
Meta
4 days ago
Adam D. Ruppe
4 days ago
monkyyy
4 days ago
jmh530
4 days ago
Meta
3 days ago
Sergey
3 days ago
Meta
2 days ago
Timon Gehr
1 day ago
Timon Gehr
2 days ago
jmh530
2 days ago
jmh530
2 days ago
Meta
2 days ago
Timon Gehr
1 day ago
jmh530
4 days ago

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

4 days ago

Apologies to Nick Treleaven as I misspelled his name in my post; unfortunately, it can't be edited.

4 days 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:

Note that this has been implemented and shipped in OpenD for a long time, many months. Your example works with the opend compiler right now; https://opendlang.org/start.html

4 days ago

On Monday, 12 May 2025 at 23:35:06 UTC, Adam D. Ruppe wrote:

>

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:

Note that this has been implemented and shipped in OpenD for a long time, many months. Your example works with the opend compiler right now; https://opendlang.org/start.html

Very nice, I didn't know that. Is that using Timon's implementation?

4 days 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;

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

Make overflowing assignments landmines rather then errors?

auto foo(T)(T t){
  auto (a,b,c)=t;
  static if(T.length>2){
    return c;
  } else {
    return a;
}}
unittest{
  assert(foo(tuple(1,'b',"c"))=="c");
  assert(foo(tuple(1))==1);
}
auto bar(T)(T t){
  auto (a,b,c)=t;
  return c;
}
unittest{
  assert(foo(tuple(1,'b',"c"))=="c");
  assert(foo(tuple(1))==1);//fails `Error: tuple!int was split into 3 fields but only has 1, then `c` used on line 15 in bar!(tuple!int)
}
4 days ago

On Monday, 12 May 2025 at 23:36:26 UTC, Meta wrote:

>

Very nice, I didn't know that. Is that using Timon's implementation?

Yes, he helped prepare it and we merged as soon as it was ready. Caused virtually zero breakage, so easy addition.

4 days 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;

(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 (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

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.

Am I right that any function that returns a tuple can use it like
‘auto (a, b) = foo();’
Because that would be nice

4 days ago

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.

>

Am I right that any function that returns a tuple can use it like
‘auto (a, b) = foo();’
Because that would be nice

Yup, that's right.

3 days ago

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

>

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

Awesome DIP!
To not have a built-in tuples in the lang is a shame!

Just to clarify, will the static arrays gonna work?

(int a, float[3] b, string c) = tuple(1, 0.1, 0.2, 0.3, "2");
3 days ago

On Tuesday, 13 May 2025 at 07:25:06 UTC, Sergey wrote:

>

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

>

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

Awesome DIP!
To not have a built-in tuples in the lang is a shame!

Just to clarify, will the static arrays gonna work?

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

I'm pretty sure that works, except it should be [0.1, 0.2, 0.3].

« First   ‹ Prev
1 2