[proposal] Automatic deref for Reference
#2594
Replies: 14 comments 16 replies
-
Based on your proposal, I am also in favor of the eager decay option. It removes the need for functions to explicitly, by default, reason about argument references, which seems like it will generally simplify ergonomics. |
Beta Was this translation helpful? Give feedback.
-
I think the proposal is very well written and personally like the eager decay model of the two options. |
Beta Was this translation helpful? Give feedback.
-
I'm also in favor of the eager decay model. |
Beta Was this translation helpful? Give feedback.
-
Eager decay model is better. In Rust, lifetimes is simply an annotation (implicit or explicit) and one of the compiler internals. Mojo takes it one step further and the entire
|
Beta Was this translation helpful? Give feedback.
-
I quite like the second-class reference ("eager decay") idea. It's not clear how I'm not sure if it's by design, it seems that we can't change the "pointee". It's a bit weird, as var a = 1
var b = 1
var r = Reference(a)
r = Reference(b) # doesn't work |
Beta Was this translation helpful? Give feedback.
-
I would be nice to get a definition for |
Beta Was this translation helpful? Give feedback.
-
As I've mentioned elsewhere, a major problem that needs to be resolved is the following:
There are three kinds of mutations that a programmer might want to perform upon
Also, Quite possibly, to resolve this issue (and also issues with the meaning of
Maybe this would cause more problems than it solves though—it could force people to write a lot of "adapter" code to switch between working with these types. If we want to stick with a single reference type, I suspect we will need a mechanism for selecting the "number of decays" that we want an expression (e.g. |
Beta Was this translation helpful? Give feedback.
-
+1 for the eager decay option if it makes things easier to learn and teach. |
Beta Was this translation helpful? Give feedback.
-
Maybe instead of associating auto-deref with the
Here, A method call whose return type is of this form would have the same affordances as an argument:
And if the caller wants to "upgrade" the return type into a first-class Virtually all functions in the standard library that currently return We'd need to consider how functions returning |
Beta Was this translation helpful? Give feedback.
-
I am also in favour of eager decay model. Developers need to worry much less about how references are interacting with rest of the code compared to the persistent model. |
Beta Was this translation helpful? Give feedback.
-
I'm ok with eager decay |
Beta Was this translation helpful? Give feedback.
-
Yes, decay is like ASAP, nice ! 👍 It would be helpful for new-comers too by reducing the amount of brackets. Great 🎉 |
Beta Was this translation helpful? Give feedback.
-
honestly, I believe the "eager decay" way is more ergonomic, but it fits better how C++ does things, where you can't directly reference a reference, so you need to use the "reference_wrapper" for this. My feeling is that referencing a reference is such a niche thing that a wrapper would be fine. But, we're kinda stuck, since "Reference" is the perfect name for it already. Maybe we could add some more reference structs to Mojo if it doesn't overcomplicate things? let ri: Inout[T] = ... // Mutable reference, automatically decays when needed
let rb: Borrowed[T] = ... // Immutable reference, also automatically decays
let rr: Reference[...] = ... // Reference wrapper, no automatic decay Another option is that decay could only happen in specific situations like function calls, returns, or when the assignment target naturally expects the underlying type. For example: var s = "Hello World!"
var r1 = Reference(s) // Reference[String]
var r2 = Reference(r1) // Reference[Reference[String]]
var s1: String = r2 // Decay happens here, s1 gets the original string
var s2: Reference[String] = r2 // Decay happens here, s2 gets Reference[String]
var s3 = r2 // No decay, s3 is Reference[Reference[String]]
var s4: Reference[...] = r2 // No decay, s4 is Reference[Reference[String]] |
Beta Was this translation helpful? Give feedback.
-
For thread hygiene, I'll close this - the design has evolved, keeping with the "eager decay" concept, but with a different implementation over here: #2874 |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I put together a proposal to outline how automatic deref of the
Reference
type can work in Mojo, I'd love thoughts or comments on it. I'm hoping to implement this in the next week or two:https://github.com/modularml/mojo/blob/main/proposals/auto-dereference.md
-Chris
Beta Was this translation helpful? Give feedback.
All reactions