Let’s say I define some operators for my class like this:
class A {
def +(f: Float) = /* ... */
}
val a: A = new A
This allows me to do a + 1f, easy enough. What if I want to enable the lib’s user to be able to write 1f + a, too? How can I implement that?
In Scala 2.9 you can import this implicit conversion:
and use it as you wanted. Since Scala 2.10 you better do this conversion like so:
or even better like so:
The last way is called Value Class and in difference to preceding two it provides this functionality with zero overhead. (Thanks, axel22) This is also the new stuff that comes with 2.10
Or you can just modify
Alike so:and use it like so:
The last approach is preferable.