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.
@switch
annotation in Scala 2.8:Example:
Implicit definitions, particularly conversions.
For example, assume a function which will format an input string to fit to a size, by replacing the middle of it with "...":
You can use that with any String, and, of course, use the toString method to convert anything. But you could also write it like this:
And then, you could pass classes of other types by doing this:
Now you can call that function passing a double:
The last argument is implicit, and is being passed automatically because of the implicit de declaration. Furthermore, "s" is being treated like a String inside sizeBoundedString because there is an implicit conversion from it to String.
Implicits of this type are better defined for uncommon types to avoid unexpected conversions. You can also explictly pass a conversion, and it will still be implicitly used inside sizeBoundedString:
You can also have multiple implicit arguments, but then you must either pass all of them, or not pass any of them. There is also a shortcut syntax for implicit conversions:
This is used exactly the same way.
Implicits can have any value. They can be used, for instance, to hide library information. Take the following example, for instance:
In this example, calling "f" in an Y object will send the log to the default daemon, and on an instance of X to the Daemon X daemon. But calling g on an instance of X will send the log to the explicitly given DefaultDaemon.
While this simple example can be re-written with overload and private state, implicits do not require private state, and can be brought into context with imports.
Implicit arguments in closures.
A function argument can be marked as implicit just as with methods. Within the scope of the body of the function the implicit parameter is visible and eligible for implicit resolution:
Traits with
abstract override
methods are a feature in Scala that is as not widely advertised as many others. The intend of methods with theabstract override
modifier is to do some operations and delegating the call tosuper
. Then these traits have to be mixed-in with concrete implementations of theirabstract override
methods.While my example is really not much more than a poor mans AOP, I used these Stackable Traits much to my liking to build Scala interpreter instances with predefined imports, custom bindings and classpathes. The Stackable Traits made it possible to create my factory along the lines of
new InterpreterFactory with JsonLibs with LuceneLibs
and then have useful imports and scope varibles for the users scripts.Case classes automatically mixin the Product trait, providing untyped, indexed access to the fields without any reflection:
This feature also provides a simplified way to alter the output of the
toString
method:You can define your own control structures. It's really just functions and objects and some syntactic sugar, but they look and behave like the real thing.
For example, the following code defines
dont {...} unless (cond)
anddont {...} until (cond)
:Now you can do the following: