DIP for multiple auto ref values
Stefanos Baziotis
sdi1600105 at di.uoa.gr
Tue Apr 9 22:41:41 UTC 2019
On Tuesday, 9 April 2019 at 21:05:25 UTC, Timon Gehr wrote:
> Expanded tuples already exist, but you can't return them from
> functions:
True, with Ilya we meant incorporate it so that one can return
them.
> 2) It would need special name mangling.
That is an issue, I haven't thought of that, thanks!
>
> The DIP could try to lift this restriction while also allowing
> multiple `ref` return values.
Yes, pretty much that was one of the proposals in -- FINALLY -- on
the original post
>
> 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)
I don't know how to tackle those yet but thanks for mentioning
cause
I haven't thought of them.
>
> 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
Actually, just know I realized that you're the one with the tuple
DIP
mentioned previously.
I didn't know about that DIP, this indeed seems a good enhancement
to that DIP.
> ---
> 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);
> }
Yes, that is a possible syntax (that I have mentioned in the
discussion
with Ilya, I might have forgot to include it in the summary
post). I should
just mention again though that this thing can't be done by simply
incorporating tuples because they don't support refs
because ref is not a type. One of the important points of the DIP
that I try to think is how to support similar syntax (and the
proposals
were the first thoughts of Ilya and me).
> ---
>
> ---
> 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);
> }
That is another possible syntax. This example actually looks
somewhat like Ilya's.
> ---
>
> import std.range, std.algorithm; // (need to be changed)
> void main(){
> auto a=[1,2,3,4,5];
> foreach((i,ref j);enumerate(a)){
> j=i;
> }
> assert(a==[0,1,2,3,4]);
> }
This might be a DIP in and of itself because I believe what
you're writing here is an improvement on foreach that will
actually require the ability to return multiple ref values.
I might be wrong though, look the 3rd point of Ilya in this [1]
post.
> Are you interested in DIP writing only or also compiler
> implementation?
Actually, I think I will be way better in compiler implementation
because (as it is probably evident), I'm quite bad
in DIP writing (I should mention that I learned about D before 3
weeks).
Of course I will try to do a good job on both but I might need
some
help in the DIP writing.
> 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.
That's great!
[1]
https://forum.dlang.org/post/shqiuqfegpbrnjtzyvwf@forum.dlang.org
More information about the Digitalmars-d
mailing list