Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Scala's "magic" functions

Tags:

syntax

scala

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.

like image 274
Elazar Leibovich Avatar asked Sep 27 '09 09:09

Elazar Leibovich


People also ask

What are the functions in scala?

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.

What is the meaning of => in scala?

=> 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 .

What is the type of a function in scala?

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.

What is Python magic method?

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.


1 Answers

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.

like image 107
Daniel C. Sobral Avatar answered Oct 14 '22 08:10

Daniel C. Sobral