This is an old revision of the document!


8 - Implicits

Another powerful Scala feature today.
Implicits do a lot of black magic behind the scenes.
Today we learn how to control the magic.

You can find today's contents here.

Practice

We'll extend an existing well-known type with a few methods, use implicits to sort collections of our own datatypes and finally try to improve equality operations in Scala.

  1. Define the class RichInteger that takes an Int parameter:
    class RichInteger(val x: Int)
    • Define the sqrt method on it. It should do the square root of x. Use an implicit conversion from Int to String to enable this operation on integers.
    • Convert the previous code to an implicit class. How does this help you?
    • Add an isPrime method to RichInteger. It should check whether x is prime or not.
    • Add the gcd method to RichInteger. It should receive another y parameter of type Int and compute the greatest common divisor of the two.
    • Test the new magic methods you just created.
  2. Define a data type for semantic versioning.
    • Use a case class and name it Version. It should have 3 fields, all integers: major, minor and patch.
    • Define the < operator on the new data type.
    • Define a value for a Version ordering. Its type should be Ordering[Version]
    • Create a list of versions. Try calling the sorted method on it. Does it work?
    • Promote the ordering defined earlier to an implicit value. What's the recommended place to put the implicit?
  3. Define the Eq typeclass to offer support for equality testing.
sesiuni/scala/lab8.1467812766.txt.gz · Last modified: 2016/07/06 16:46 by ataleanu