idea for Native D Tuple Syntax, packing and unpacking

Kindly Doright kindly.doright at no-e-mail.com
Tue Jun 25 10:40:18 UTC 2024


On Sunday, 23 June 2024 at 04:10:35 UTC, Kindly Doright wrote:
> // idea for Native D Tuple Syntax, packing and unpacking

// idea for Native D Tuple Syntax, **revision A.1**
// (strictly for packing and unpacking a single-layer grouping of 
items)

/+
Purpose: to pass or return a group of unsimilar items
(without the need for designing and implementing a data structure)

**GOAL:** to **reduce cognitive load**, to simplify coding effort

Other: *simplify compiler implementation* and *provide a distinct 
language construct*
Concept Synonyms: pack/unpack, group/ungroup, marshall/unmarshall
+/

```d
// Pack & unpack a single-layer grouping
// #() = #();
#(a, b, c, d) = #(1, 'strval', [1,2,3], true);

// myTuple = #(values); #(values) = myTuple;
auto y = #(1, 'strval', [1,2,3], true);
#(a, b, c, d) = y;

// As a single-layer unpack, auto can be specified
auto #(a, b, c, d) = #(1, 'strval', [1,2,3], true);
#(a, b, auto c, d) = #(1, 'strval', [1,2,3], true);
```

```d
// Optional unpack: skip value assignment (D-style)
#(a, ...) =  #(1, 'strval', [1,2,3], true); // a = 1
#(_, b, _, d) =  #(1, 'strval', [1,2,3], true); // b = 'strval', 
d = true
auto #(_, b, _, d) =  #(1, 'strval', [1,2,3], true);

#(_, b, ...) =  #(1, 'strval', [1,2,3], true); // b = 'strval'
#(_, auto b, ...) =  #(1, 'strval', [1,2,3], true);

// **Invalid Syntax** for optional unpack assignment
// reason: unnecessary complexity
#(a, ..., d) =  #(1, 'strval', [1,2,3], true);
```

```d
// **Invalid Syntax** embedded pack Tuple Syntax (i.e. 
multi-layer)
// reason: multi-layer pack --- #(5, 'str2')
#(a, b, c, d, eee) = #(1, 'strval', [1,2,3], true, #(5, 'str2')); 
// has *multi*-layer pack

// Valid Syntax (single-layer pack)
auto myTuple = #(5, 'str2');
#(a, b, c, d, eee) = #(1, 'strval', [1,2,3], true, myTuple); // 
single-layer
#(f, g) = eee; // f = 5, g = 'str2'

// **Invalid Syntax** embedded unpack Tuple Syntax
// reason: multi-layer unpack --- #(f, g))
auto myTuple = #(5, 'str2');
#(a, b, c, d, #(f, g)) = #(1, 'strval', [1,2,3], true, myTuple); 
// has *multi*-layer unpack

```

```d
// **Invalid Syntax** misuse of pack Tuple Syntax
// reason: the packing syntax does not function as a Tuple 
variable
// (the pack syntax does not function as an intermediate Tuple 
value; it is strictly for assignment)
auto y = #(1, 'strval') + #([1,2,3], true); // **misuse of 
syntax**
y += #(5, 'str2'); // **misuse of syntax**
foreach (int item; #(1, 'strval')) { writeln(item); } // **misuse 
of syntax**

// Valid use of Packing Syntax
#(a, b) = #(1, 'strval');
auto y = #(1, 'strval');
return #(1, 'strval');

// **Invalid Syntax** misuse of pack Tuple Syntax
// reason: use of Packing Syntax as a function parameter
myfuncCall('staticParam', #(1, 'strval'));

// Valid use
auto y=#(1, 'strval');
myfuncCall('staticParam', y);

```



More information about the dip.ideas mailing list