Jump to page: 1 24  
Page
Thread overview
Tuple Unpacking Syntax
May 12
Meta
May 12
Meta
May 12
Meta
May 12
monkyyy
May 13
jmh530
May 13
Meta
5 days ago
Nick Treleaven
5 days ago
jmh530
May 13
Sergey
May 13
Meta
May 20
Ogion
5 days ago
Nick Treleaven
May 14
jmh530
May 14
jmh530
May 14
Meta
May 15
jmh530
May 20
jmh530
May 21
jmh530
6 days ago
Timon Gehr
5 days ago
Atila Neves
5 days ago
Meta
2 days ago
Timon Gehr
2 days ago
M. M.
May 12

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

May 12

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

May 12

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

May 12

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?

May 12

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)
}
May 12

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.

May 13

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

May 13

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.

May 13

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");
May 13

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 3 4