-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[superseded] new syntax for lvalue references: byaddr x = expr
#13342
Conversation
Things like these must go through the RFC process, if only so that people have a better chance to vote on them. And personally I would have used |
17b477a
to
1720bae
Compare
well that was the conclusion from #11824 (comment)
(and I've opted for |
Well I'm sorry, but not everything I say has a chance of being accepted immediately. Sometimes I'm just throwing ideas around. |
I believe this capability is present in the more comprehensive proposal here: Due to the way type classes interact with type inference, once the proposal is implemented you'll be able to write code like the following: let x: lent = foo()
var y: var = bar() |
Note that I cannot stand let x: lent = foo()
var y: var = bar() Nim is about type inference. However, something like this: let x = lent foo()
var y = var bar() is perfectly fine. IMHO anyway. |
let x0 = @[1,2]
var y0 = @[1,2]
let x = lent x0[0]
var y = var y0[0]
y+=10
doAssert y0 == @[11, 2]
doAssert type(x) is int
doAssert type(y) is int
|
IMO, lent/var is determined by return type of foo() / bar () / not by the caller of the function. Caller then can unwrap lent T to T if he likes. I would suggest call it |
Yes but the caller can extend the "var/lent-ness" via my proposed annotations. |
What does this do? Create expression aliases? |
analog of C++ references in @zah in your proposal, would the asserts pass in #13342 (comment) ? also
|
@timotheecour, yes, these asserts should pass. The type equivalences are slightly more dubious, but that's what Nim currently does: type Foo = object
proc bar(x: Foo, y: var Foo) =
echo x is Foo # true
echo y is Foo # true Here, the types of |
The main idea of the other proposal is that it comes with a set of rules that make these short-lived aliases safe. Enforcing the rules is the most significant challenge here. The syntax is less important, but again, the other proposal is a bit more versatile covering the full spectrum of var, lent and openarray pointers. If we accept the feature without enforcing the rules, we'll need to either stick with unsafe code forever or introduce a breaking change in the future. |
superseded by #13508 |
byaddr x = expr
byaddr x = expr
supersedes [superseded] ref syntax for lvalue expressions:
byRef: myref=x[1].foo[2]
#11824 ; exactly same idea but with a parser change to allow the cleaner syntaxbyaddr x = expr
instead ofbyaddr: x = expr
see [superseded] ref syntax for lvalue expressions:
byRef: myref=x[1].foo[2]
#11824 for rationale, examples etcbyaddr is now a new keyword and introduces a new section nkCustomDefSection, analog to nkLetSection except it has 1 more field, placeholder for the name of the section:
byaddr foo = bar
produces this AST:thanks to nkCustomDefSection, we can easily extend to additional keywords without changing much else in the compiler/parser/lexer, and more importantly user code, for example it'd be easy to add (if one wanted to):
alias foo = bar
example