tags:

views:

953

answers:

4

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 didn't find anything.

update: I thought that it goes without saying, but since someone has mistaken I'll clarify. I'm not interested with the usage of magic functions in the standard library, but in which magic functions exists.

+2  A: 

They are defined in the Scala Language Specification. As far as I know, there are just three "magic" functions as you mentioned.

EDIT: Sorry, I remember another thing may also relate to your "magic", as following: ////////////////////////////////////////////////////////////////////////////////

scala> class Magic {
 |     private var x :Int = _
 |     override def toString = "Magic(%d)".format(x)
 |     def member = x
 |     def member_=(m :Int){ x = m }
 | }

defined class Magic

scala> val m = new Magic

m: Magic = Magic(0)

scala> m.member

res14: Int = 0

scala> m.member = 100

scala> m

res15: Magic = Magic(100)

scala> m.member += 99

scala> m

res17: Magic = Magic(199)

Eastsun
If you could dig for me an evidence for that claim, you'll be answering my question ;-) I assumed it'll be in the specs, but finding them in it is not a fun job.
Elazar Leibovich
Scala Language Specification:6.15 Assignments...If x is a parameterless function defined insome template, and the same template contains a setter function x_= as member,then the assignment x = e is interpreted as the invocation x_=(e ) of that setterfunction. Analogously, an assignment f .x = e to a parameterless function x isinterpreted as the invocation f .x_=(e ).An assignment f (args) = e with a function application to the left of the “=’ operatoris interpreted as f .update(args, e ), i.e. the invocation of an update functiondefined by f .
Eastsun
I ment, evidence that there're no more.
Elazar Leibovich
+7  A: 

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.

Daniel Spiewak
+12  A: 

As far as I know:

Getters/setters related:

apply
update
identifier_=

Pattern matching:

unapply
unapplySeq

For-comprehensions:

map
flatMap
filter
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.

And I don't believe there's any single place where all of Scala's syntactic sugars are listed.

Daniel
You might want to add the unary_! etc. operators that were listed in one of the other posts, since this looks like the most exhaustive answer to the question here :)
Calum
...and you've done it, thanks!
Calum
+1  A: 

Sorry if it's not exactly answering your question, but my favorite WTF moment so far is @ as assignment operator inside pattern match. Thanks to soft copy of "Programming in Scala" I found out what it was pretty quickly.

Using @ we can bind any part of a pattern to a variable, and if the pattern match succeeds, the variable will capture the value of the sub-pattern. Here's the example from Programming in Scala (Section 15.2 - Variable Binding):

expr match {
  case UnOp("abs", e @ UnOp("abs", _)) => e
  case _ =>
}

If the entire pattern match succeeds, then the portion that matched the UnOp("abs", _) part is made available as variable e.

And here's what Programming Scala says about it.

Yardena
Can you share with us what the @ operator does?
Elazar Leibovich
Yes, just edited my answer :-)
Yardena