List of Scala's "magic" functions
Where can I find a list of Scala's "magic" functions, such as apply
, unapply
, update
, +=
, etc.?
By magic-functions I mean functions which are used by some syntactic sugar of the compiler, for example
o.update(x,y) <=> o(x) = y
I googled for some combination of scala
magic
and synonyms of functions
, but I didn't find anything.
I'm not interested with the usage of magic functions in the standard library, but in which magic functions exists.
Solution 1:
As far as I know:
Getters/setters related:
apply
update
identifier_=
Pattern matching:
unapply
unapplySeq
For-comprehensions:
map
flatMap
filter
withFilter
foreach
Prefixed operators:
unary_+
unary_-
unary_!
unary_~
Beyond that, any implicit from A to B. Scala will also convert A <op>= B
into A = A <op> B
, if the former operator isn't defined, "op" is not alphanumeric, and <op>=
isn't !=
, ==
, <=
or >=
.
And I don't believe there's any single place where all of Scala's syntactic sugars are listed.
Solution 2:
In addition to update
and apply
, there are also a number of unary operators which (I believe) qualify as magical:
unary_+
unary_-
unary_!
unary_~
Add to that the regular infix/suffix operators (which can be almost anything) and you've got yourself the complete package.
You really should take a look at the Scala Language Specification. It is the only authoritative source on this stuff. It's not that hard to read (as long as you're comfortable with context-free grammars), and very easily searchable. The only thing it doesn't specify well is the XML support.