All of the commentary is helpful, and I can’t say I disagree with anyone’s thoughts on it.
Honestly, in my own opinion, map { |file| File.open(file, "r") }
is better written exactly that way. It feels to me that the shortcut should be for the most common case, which I think is to only have a single argument. I can keep track of one argument to one method in my head when I’m reading someone else’s code (or my own code the day after I wrote it); more than that feels like it deserves real parameter names.
I am a very experienced perl programmer, I’ve used it since Perl 4, and I love the language, but the thing I like about Crystal is that it encourages me to use named arguments, and never have $_[13]
creep into my code. (That’s perl for 13th positional parameter, for those who don’t know the language.)
The basic appeal to me of .map(&.to_i)
is not just that it’s concise, but also that there’s no whitespace, so it feels like a single expression. That’s what I’d love to have for the method parameter case.
For that reason I don’t love &foo(&1)
. It isn’t much shorter than writing it out as {|x|foo(x)}
, except for the whitespace the formatter will put into it. And &foo
looks a lot like &.foo
, which might confuse future me.
The it
option like &foo(it)
is fine, but I agree with some comments that it
doesn’t stand out enough as a placeholder. What happens if I have it=1; x.map(&foo(it))
?
That’s why I thought maybe ^.foo
or something similar would make me happy. It’s visually unambiguous, there’s only one possible parameter, so no creeping into $_[13]
territory, and it covers a what is probably 30-40% of my uses of #map
and #each
and similar operations on enumerables.
When I get time™ I might look at opening a new RFC for this simple case, with a single argument. And if the community goes a different direction, that’s fine, but hopefully it’ll be possible to conserve this concise, single-parameter case.
(And I’m torn on whether I’d like to see ^.foo.bar
be allowed, and expanded to { |obj| foo(obj).bar }
… it feels like a common case for me, but in reality, how often does it exist?)