New Modifier: 'instance' for the ref keyword #7198
Unanswered
TactiTac0z
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
Ref fields on the heap don't work, even if those refs are constrained to refer to heap memory, because of the "interior pointers" problem. I don't recall the exact reasoning, but I think it's something like: having more than one address refer to the same object on the heap, such as an ordinary value of a reference type versus a |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We add a new modifier which can be placed before 'ref' similar to 'scoped'.
an 'instance ref' variable, field (etc) is a ref but is constrained to only point to heap memory (aka only point to memory inside a class instance, array element, or static field.)
How does that help us?
This allows us to have ref fields for classes and structs.
This may be pretty useful for asynchronous methods, you can imagine how asynchronous methods can use ref instance values just fine, since we don't have lifetime issues.
using ref within asynchronous methods is not allowed at all right now because the context of the method of course needs to be saved on the heap which would immediately break a by-ref variable containing a reference to the stack.
Imply 'instance'?
Add 'instance' modifier to struct method declarations as well such that fields of that type can be passed by instance ref within that method?
Callers of this new TestMethod method would then need a reference of 'instance' ref TestStruct to call the method.
The example of the double 'instance' modifier above brings up another question.
Should the 'instance' modifier be after 'ref' semilar to 'readonly'?
This fixes an ambiguity issue of
instance ref Method() {}
It would be similar to 'readonly' but not similar to 'scoped', but for ambiguity reasons it may have to be at the end not in front.
I'm open to use a different keyword instead of 'instance' like maybe 'managed', 'heap', etc?
This feature does need runtime changes, but I think most issues regarding holding the referenced object alive for the gc have been solved with the addition of ref fields for ref structs already.
Beta Was this translation helpful? Give feedback.
All reactions