Discusson about &

Hello, &.call is the same thing of calling method on first arg,
is it same as &:call in ruby?

Coming from ruby background, I’m quite confused by that,
I have to mentally translate &.call to &:call,
in ruby &. means safe navigation operator

&sth means calling to_proc on it
so foo(&sth) →
if sth is already a proc, to_proc is same as itself, otherwise calls to_proc on it,
you can &:foo because Symbol has to_proc

I’m not sure I follow what the question is, or what you’re suggesting?

some_method &.foo is the same as some_method { |x| x.foo } in Crystal. if thats what &:foo does in ruby, then yes they’re the same. Could consider adding that to Crystal for Rubyists - Crystal.

It’s the same idea as the Symbol#to_proc shorthand in Ruby, yes. But it’s also more powerful in that you can pass arguments and chain method calls. In Ruby, if you want to do that, you can’t use the shorthand.

# foo { |x| x.call("bar").baz }
foo(&.call("bar").baz)

One place this is super useful is in Log in the standard library. When you log things, the block yields a Log::Emitter which has an emit method so you can pass metadata for your log entries:

log.debug &.emit "log message", foo: "bar", omg: "lol"