-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Parser: allow ->@ivar.foo and ->@@cvar.foo #9268
Parser: allow ->@ivar.foo and ->@@cvar.foo #9268
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems simple enough
ping. |
If we are allowing a new syntax, or supported case, we should add a spec regarding its semantics. So we would need something in In particular what happens if the ivar or cvar changes values between the proc creation and the invoke? |
The value (not reference) of ivar is captured. It is same as usual class Foo
def initialize
@foo = "foo"
proc = ->@foo.upcase
@foo = "bar"
p proc.call # => "FOO"
end
end
Foo.new |
Specs are added. |
I'm hesitant if the semantic should be to not capture the ivar value, but the self value. The semantic of this pr can be achieved by assigning the ivar to a local variable. class Foo
def initialize
@foo = "foo"
foo = @foo
proc = ->foo.upcase
puts proc.call
end
end The semantic of capturing self is equivalent to have proc to an instance method and a forwarding method. class Foo
def initialize
@foo = "foo"
proc = ->self.upcase
puts proc.call
end
def upcase
@foo.upcase
end
end I constantly read Having the latter semantics in I think is also more consistent with how the typing of ivars works. Asuming that it's value can be changed at any time. Maybe @hugopl can share a bit more of the background story that lead to the need for this construct. |
@bcardiff Don't you also find this confusing? foo = "hello"
proc = ->foo.upcase
foo = "bye"
proc.call # => "HELLO" That is, I think we are talking about a different problem than what this PR is trying to solve. |
Slightly, but understandable. |
The need of this construct was just because feels natural to expect the language to support To be honest, I thought the behavior of i.e. I would also expect |
Currently It seems natural that
I think so too really. Thank you. |
If |
I don't think so, because both cases have similar syntax and then should have similar semantics. Also note that if |
Makes sense, I think the language reference should be more clear about this BTW, the docs just say:
|
Now, `->var.foo` is just syntax sugar of `-> { var.foo }`. It is PoC and an objection against @waj's comment. crystal-lang#9268 (comment)
Fixed #9239
Now, we can compile such a code: