Iām not sure how allowing to use an arbitrary, undeclared name would be familiar to people coming from any other language, because I havenāt seen this feature anywhere else. In Haskell, the lambda parameter name is explicitly declared, so Iām not sure why this would be familiar to Haskell users.
Also, real code is never a single line. If I encountered such a line in an actual program, Iād have to spend a lot of time trying to understand where ānameā comes from - is it a variable from an outer scope? inherited from a base class? imported via an alias? When I see āitā, itās immediately clear what this means.
Not to mention all the confusion that would arise if you meant to refer to a specific function or variable from an outer scope, made a typo, and Kotlin would interpret it as the lambda parameter according to the āa parameter name to your likingā rule that you propose.
You are right I mixed something up. Haskellās case of allows to destructure functions and assign arbitrary names:
case box of
Just content -> print content
Nothing -> print "Nothing inside"
For regulary functions you still need to declare them. Maybe I was just confused because I didnāt remember having to find a keyword to refer to the only element in Haskell:
map toUpperCase ["Bob","Eva","Zoe"]
Is there a similar way of achieving that? Java allows method references.
it as variable name is cruel, fun as short form of function is awful, @annotations notate all the stuff where the language has no better solutions
BUT using the $dollarsign as variable prefix is even worse then all above
give no requirements for variable name and highlight it in the IDE then.
It is still better than just original idea because no problem with sensibility will appear.
Of course, it is a bad idea to use it big lambdas.
I suggest you stick to using it for only single-line lambdas (that donāt contain further nested lambdas), and explicit names everywhere else. Anything else is probably going to introduce more problems than it solves. Having to add cat -> isnāt significant burden.
I see. The thing I mean is connected with similarity of programming languages and natural languages.
I suppose
/*When I found that */pets.all { it is Cat }
cats.map { cat -> cat.name } to be similar to āpets where for a cat I choose name of the catā
.map { it.name } to be similar to āpets where I choose name of the themā
.map ${ $cat.name } to be similar to āpets where I choose name of the the catsā
So I find first sentence very innatural, the second does not say that those pets are cats, so I suggested to create something like what is written in the last one.
I would always prefere explicit name ā name.toUpperCase just not to be ambiguous. What if someone inserts a line name = āanonymousā before your line of code. How should that code behave?
You would not know what the writerās intention was. Itās nice not to read much useless code but imho even when it was possible you should not use it
When using the last mentioned approach, (.map ${ $cat.name }) you donāt have that problem because the usual variable can not be named with $someName.
You are right. With your approach you do not have the problem I saw with the original post. But Iād still prefer the lambda with explicit cat ā ā¦
Imho you should care about the most readable and therefore maintainable code. About taste can not be argued