Replies: 7 comments 5 replies
-
Devil's advocate: |
Beta Was this translation helpful? Give feedback.
-
There's a default (capture by value). |
Beta Was this translation helpful? Give feedback.
-
@cpurdy can you describe the difference between captured by var and by ref. |
Beta Was this translation helpful? Give feedback.
-
Some possibilities, I tried to fill out the quadrant. Surely misses some cases, and didn't compute it write. Feel free to correct or add more cases. Just reads, capture by valuefor( Int i: 0..4 )
funs += () -> print(i,i);
for( fun : funs )
funs(); Always Ef-Final - "0,0 1,1 2,2 3,3" Just reads, capture by Var/Ptrfor( @CaptureByVal Int i: 0..4 )
funs += () -> print(i,i);
for( fun : funs )
funs(); Always Ef-Final - "4,4 4,4 4,4 4,4" Read/Write, some choices followfor( Int i: 0..4 )
funs += () -> print(i,++i);
for( fun : funs )
funs(); Always Ef-Final - Compile error, "x++" not final Read/Write with
|
Beta Was this translation helpful? Give feedback.
-
Gene and I are discussing a path forward on this topic. We both agree that introducing a declaration site annotation makes sense. It would indicate to the compiler that the annotated variable could be captured as a Ref -- or even Var -- if and as necessary. Without an annotation, all captures would be strictly by-value captures, and thus if you were to attempt to modify a captured value that is not declaration-site annotated to indicate by-reference, then that attempted modification would be a compile-time error. Edit: Additionally, it will be a compile time error if a variable is marked as So at this point, we just need a name. Gene has fallen in love with I personally like Any other ideas/suggestions? |
Beta Was this translation helpful? Give feedback.
-
Also worth pointing to to the "Go lang" issue that originally got us talking about this change: https://kkentzo.github.io/2021/01/21/golang-loop-variable-gotcha/ |
Beta Was this translation helpful? Give feedback.
-
From the call today:
Ref
if the lambda reads from but does not write to the variableVar
(note:Var : Ref
) if the lambda writes to the variableWe want to alter these rules as follows:
&
operator) will capture by eitherRef
orVar
as appropriate (to be defined)Ref
orVar
Var
would be a compile-time errorRef
vs.Var
and just use capture byVar
instead, sinceVar : Ref
(with the possible exception being when capturing something that does not have aVar
but does have aRef
?)Declaration site syntax could be an annotation (as discussed):
@AllowCaptureByVar Int i = 0;
(Obviously the annotation name given here is for illustration purposes only; if we go this route, the name needs to be much more succinct.)
We didn't have any specific syntax suggestions for use-site syntax that I can recall.
Next step is to mock up specific syntax examples and discuss.
Beta Was this translation helpful? Give feedback.
All reactions