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.
In scala, functions are first class values. You can store function value, pass function as an argument and return function as a value from other function. You can create function by using def keyword. You must mention return type of parameters while defining function and return type of a function is optional.
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
Scala functions are first-class values. Scala functions are first-class values. You must mention the return type of parameters while defining the function and the return type of a function is optional. If you don't specify the return type of a function, the default return type is Unit.
Magic methods in Python are the special methods that start and end with the double underscores. They are also called dunder methods. Magic methods are not meant to be invoked directly by you, but the invocation happens internally from the class on a certain action.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With