Replies: 14 comments 21 replies
-
Great Proposal! I have two things which popped up in my mind.
|
Beta Was this translation helpful? Give feedback.
-
Rename
|
Beta Was this translation helpful? Give feedback.
-
I greatly appreciate the enhanced ergonomics provided by the new ref return type for users interacting with the questionsI do have some questions and concerns I'd like to ask:
Some thoughtsI believe the previous proposal allowing auto-deref for user-defined types holds more potential. I am hopeful that a solution to this issue exists. Disclaimer: preventing auto-dereferencingIntroducing "no-auto-deref variables" with a special syntax can effectively disable auto-dereferencing when working with these variables: var x = 1
var &a = Reference(Reference(x)) # No auto-deref since `a` is declared with `&` (consider postfix `&` or other syntax).
var b = a # Reference[Reference[Int]], since `a` is declared with `&` it doesn't auto-deref.
var c = b # auto-deref to Int, equivalent to a[][]
var d: Reference[Int] = b # Control decay by providing an explicit type annotation, equivalent to a[]
var e = List(b, d) # List[Int] since `b` and `d` auto-deref.
var f = List[Reference[Int]](b, d) # List[Reference[Int]] control auto-deref with type annotation.
var g = List(a) # List[Reference[Reference[Int]]] since `a` doesn't auto-deref. system programmers who wish to work with raw references without auto-deref, can prefix variable declarations with &, similar to var lst: List[Reference[Reference[Int]]]
for x in lst: ... # `x` is Int
for &x in lst: ... # `x` is Reference[Reference[Reference[Int]]]
var [a, *b] = lst # `a` is Int, `b` is Iterable[Int]
var [&a, *&b] = lst # `a` is Reference[Reference[Int]], `b` is Iterable[Reference[Reference[Int]]] the rules:
complexityIf simplicity remains a concern, and we aim for even simpler rules, we can eliminate rule 3 entirely, as it's not essential for complete functionality. This effectively reduces the rules to: There are 2 variable types:
This essentially shifts the decision of auto-dereferencing from the integration with
|
Beta Was this translation helpful? Give feedback.
-
Good job! about names, just an idea: SafeReference for Reference and UnsafeReference for UnsafePointer. That way, maybe less confusion as to why Reference can be made with an UnsafePointer and so on ? But this is just an idea, excellent job ! 🔥 |
Beta Was this translation helpful? Give feedback.
-
+1 on renaming Reference to "SafePointer" rather than just "Pointer". Due to C and C++, pointers, by themselves, generally have negative connotations. it'll be great to convey at a glance (to both low-level and high-level programmers) that this pointer is different and a lot safer. |
Beta Was this translation helpful? Give feedback.
-
This is a great proposal! I want to voice my support for the proposed keyword alternatives for
|
Beta Was this translation helpful? Give feedback.
-
By virtue of the fact that Mojo already positions itself as a safe language, I believe that prepending |
Beta Was this translation helpful? Give feedback.
-
I think we should have less names. I'm not familiar with concrete implementations of languages so I might be completely off. But to me it looks like we only really need 3 concepts:
Why not have 3 types that describe exactly that and have auto-deref? struct MyInt:
fn __init__(self: MutRef[Self], value: Int): ...
fn __add__(self: Ref[Self], rhs: Self) -> Self: ...
fn __iadd__(self: MutRef[Self], rhs: Self): ...
fn map(self: ConsumeRef[Self]) -> Self: ...
fn __del__(self: ConsumeRef[Self]): ... And simpler to reason about lifetime notation (Lifetime should be able to be constructed from the Ref subtypes) fn some_func(arg1: Ref[Int], arg2: MutRef[Int]) -> Ref[String]:
if arg1 > arg2:
return Reference[String, False, Lifetime(arg1)]("bigger")
return Reference[String, False, Lifetime(arg1)]("smaller")
fn some_func2(arg1: Ref[Int], arg2: MutRef[Int]) -> Mutref[String]:
if arg1 > arg2:
return Reference[String, True, Lifetime(arg1)]("bigger")
return Reference[String, True, Lifetime(arg1)]("smaller")
fn some_func3[
a: Lifetime, b: Lifetime
](arg1: Reference[Int, False, a], arg2: Reference[Int, True, b]) -> Ref[String]:
if arg1[] > arg2[]:
return Reference[String, False, a]("bigger")
return Reference[String, False, a]("smaller") |
Beta Was this translation helpful? Give feedback.
-
Don't have time to go into details, here are some potential thinking points:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the great write up! A quick thought: |
Beta Was this translation helpful? Give feedback.
-
Status update: the first three steps of this proposal are all implemented, and should roll out in the next nightly. I'm very happy with how they fit together. |
Beta Was this translation helpful? Give feedback.
-
I'm just hoping Mojo leans harder towards value semantics so I don't have to deal with all this, or at least won't have to deal with it in the overwhelming majority of situations. |
Beta Was this translation helpful? Give feedback.
-
Could it mean that I can implement any method as a "constructor"/factory method and E.g.
Or will we restrict the P.S. I understand that |
Beta Was this translation helpful? Give feedback.
-
Thanks For the proposal. I support
|
Beta Was this translation helpful? Give feedback.
-
This is all basically working now and the discussion converged, thank you for driving this @nmsmith !! |
Beta Was this translation helpful? Give feedback.
-
Hi everyone,
@lattner and I have developed an alternative "auto-deref" proposal to the one that Chris posted a few weeks ago. The new idea is to make auto-dereferencing a result convention, rather than a feature of the
Reference
type.The proposal can be found here.
We'd love to hear feedback from the Mojo community. You can share your thoughts below.
Beta Was this translation helpful? Give feedback.
All reactions