DIP for multiple auto ref values

Timon Gehr timon.gehr at gmx.ch
Tue Apr 9 21:05:25 UTC 2019


On 09.04.19 16:37, Stefanos Baziotis wrote:
> 
> 
> Here are the two variants for a solution from my point of view:
> 1) Incorporate the expanded tuple into the language, so that no type is 
> created.

Expanded tuples already exist, but you can't return them from functions:

---
auto ref seq(T...)(return auto ref T values){
     return values;
}
---
Error: functions cannot return a tuple
---

I think the reason for this restriction is that

1) Walter originally wanted multiple return values to follow calling 
conventions similar to passing multiple parameters.
2) It would need special name mangling.

The DIP could try to lift this restriction while also allowing multiple 
`ref` return values.

> 2) Incorporate RefTuple into the language, so that when one returns 
> multiple ref values, he gets a RefTuple.
> 
> Waiting for opinions.

I would presume that RefTuple does not work properly in @safe code? Your 
DIP probably would have to support `@safe` and `return` annotations. One 
question here will again be name mangling. (Which the tuple DIP itself 
sidesteps, but it seems a bit more complicated here)


In any case, this kind of thing should be an extension of the 
work-in-progress tuple DIP:
https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md


Basically, on top of the tuple DIP, just handle this kind of thing:

---
void foo((ref int a, int b), int c){
     a=b+c;
}
(ref int, int) bar(return ref int x,int y){
     return (x,y);
}
void main(){
     auto x=(1,2), y=3;
     foo(x,y);
     assert(x==(5,2));
     x[1]+=1;
     foo(bar(x),y);
     assert(x==(6,3));
}
---

---
auto ref foo(return ref int x){
     return (x,1); // auto ref applies to individual fields if return 
value is a tuple literal
}

void main(){
     int x=2;
     foo(x)[0]+=1;
     assert(x==3);
     assert(foo(x)[1]==1);
}
---

---
import std.range, std.algorithm; // (need to be changed)
void main(){
     auto a=[1,2,3,4,5];
     foreach((i,ref j);enumerate(a)){
         a[i]=i;
     }
     assert(a==[0,1,2,3,4]);
}
---

Are you interested in DIP writing only or also compiler implementation?
My partial progress on a tuple DIP implementation is here:
https://github.com/dlang/dmd/compare/master...tgehr:tuple-syntax

It supports parts of the first three proposals from the DIP: unpacking 
variable declarations, calling functions with multiple parameters with a 
single tuple argument, and tuple literals.



More information about the Digitalmars-d mailing list