Is it possible to use a shorthand for blocks in Crystal, e.g.
my_array.sort_by(&:size)
This attempt returns an error:
... expected a function type, not Symbol
Is it possible to use a shorthand for blocks in Crystal, e.g.
my_array.sort_by(&:size)
This attempt returns an error:
... expected a function type, not Symbol
You can use this syntax:
my_array = ["123", "22", "1"]
sorted = my_array.sort_by &.size
puts sorted
=> ["1", "22", "123"]
&.
going away any time soon. we like how it feels. –
Rourke © 2022 - 2024 — McMap. All rights reserved.
&.
plays nicer than&:
– Rourke