Preserving mutability when passing to function
How to make pointer passed to a function preserve/transmit its mutability?
Or is it a conscious constraint on
Pointer
and we should use UnsafePointer
or Arc
here?25 Replies
That's a named parameter on Pointer that lets you be generic over mutability.
Thanks for the response. Actually I did try that, but didn't know what to put in the
__origin_of
.. MutableAnyOrigin does not work (as it seems to expect the origin of the a_instance itself. If the function is inside the same lexical scope of as_instance
, then we can use that. But in case it is outside, it is not possible..
I am getting
mojo 2024.10.2406 (54f70ee8)
Give me a second, I missed .type after Origin[...], but there's other issues that came up once I pasted it into vscode.
Thanks a lot! That works!
You don't need to use Pointer at all unless you have some other reason to:
Most of the use of
Pointer
is to carry a reference inside of a struct.
At least for now, myself and one other person are doing some PL work to see if Pointer can be the main reference type.I used MutableAnyOrigin in place of MutableOrigin and it was not a type but a value... I got confused after that 🙂
MutableAnyOrigin
means "The mutable origin that might access any memory value".
It's effectively only useful inside of things like garbage collectors which can touch arbitrary memory.Understood. Thanks!!
Actually I wanted to explain the usage of Mojo's Pointer in my book.
Aha
You are brave to write a book on a language this in flux.
I am writing what I am learning. I had to do quite a few refactoring though 😆
inout and borrowed are probably going away.
What we have in their place is still the subject of debate.
Yes - I am actually following the conversations... Another set of refactorings on the way 🙂
I like your idea to keep reference a library type. Though also like Nick's syntax. A combination of both would be greaet
We're discussing it.
Some of the semantics that Nick's proposal imposes for references have issues when you are doing multi-device or close to the metal work.
I am not a PL expert - but could
ref
and the others be just syntax sugar over a library defined ReferencePointer?I think it's a good start, but ideally getting references moved out to library code would let people experiment without needing massive ecosystem changes every time.
That was more or less the idea, except that there would be a way to change what the backing type is.
So that you can have multi-node references (ex: top bits of the pointer are used to indicate which supercomputer node the data lives on) or signed pointers (for high security systems, you can add a cryptographic signing for pointers which go between devices the ensure that access is authenticated).
We can have that, and have a "default" referencepointer which is blessed with a syntax... The default one would be enough for 95% of cases...
So being a Reference is a "trait" as you suggest, but there is a default implementation used by the compiler when we use
ref
This would need to be a language level switch most likely, so you have to recompile the whole standard library.
Or, it might be easier to just bless the type and let users modify the ReferencePointer type to their needs so long as they keep the API the same.
Or some kind of a preprocessor like:
But then we need to be super carefull that the different reference types interoporate seamlessly.
It probably needs to be a compiler switch.
Yes. Most likely
But most developers would not have to worry.
Yes, the usecases for overriding what "ref [_]" maps to are very small.
Something I've always wondered... Is anybody from Modular involved in this discussion or will you guys come up with a proposal Chris will hopefully accept at some point?
Nobody from Modular has given feedback yet, so it's just me and Owen doing work right now as far as I know.