What are the hidden features of Scala that every Scala developer should be aware of?
One hidden feature per answer, please.
What are the hidden features of Scala that every Scala developer should be aware of?
One hidden feature per answer, please.
require
method (defined inPredef
) that allow you to define additional function constraints that would be checked during run-time. Imagine that you developing yet another twitter client and you need to limit tweet length up to 140 symbols. Moreover you can't post empty tweet.Now calling post with inappropriate length argument will cause an exception:
You can write multiple requirements or even add description to each:
Now exceptions are verbose:
One more example is here.
Bonus
You can perform an action every time requirement fails:
Dunno if this is really hidden, but I find it quite nice.
Typeconstructors that take 2 type parameters can be written in infix notation
placeholder syntax for anonymous functions
From The Scala Language Specification:
From Scala Language Changes:
Using this you could do something like:
Build infinite data structures with Scala's
Stream
s : http://www.codecommit.com/blog/scala/infinite-lists-for-the-finitely-patient