Wat? Scala
-
1 min read
As I got quoted recently in “Scala — 1★ Would Not Program Again” I though I finally should write up a little Wat moment we had recently:
So does anyone know “wat” the following Scala code returns? (Value and Type)
List(1,2,3).toSet()
A Set<Int> containing 1,2,3?
Nope how about I give you a clue, there are 2 bugs in this one line:
- A type inferencing bug where it chooses Set<Any>
- A bug where the brackets are used for both calling the Set.apply method and constructing Unit, notice there no space between the “toSet” and “()”
Yup you guessed it, it returns:
false
Wat? Try it in your repl and for even more fun check the bytecode out.
UPDATE:
Looks like ("-Yno-adapted-args", “Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver.”) is your friend
Pete Kneller has done some really good analysis so you can see all the different weird combinations