Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Scala implicit class based on type class

implicit class IntIncrement(val underlying: Int) extends AnyVal {
    def increment(): Int = underlying + 1
}

This is valid and allows me to do something like 1.increment()

I want to be able to constrain a type parameter to have this .increment() method on it, so I started doing this:

trait Increment[T] {
    def increment(value: T): T
}

object Increment {
    implicit val implInt: Increment[Int] = new Increment[Int] {
        def increment(value: Int): Int = {
            value + 1
        }
    }
}

def increment[T](value: T)(implicit valueIntDec: Increment[T]): T = {
    valueIntDec.increment(value)
}

issue is, this increment method only allows for increment(1) instead of 1.increment()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

is there any way to create an implicit class for any T that has an implicit of Increment[T]

implicit class ImplicitIncrement[T](val underlying: implicit Increment[T]) extends AnyVal {
    def increment(): T = increment(underlying)
}

something like this ^^

>Solution :

You can do that, just without AnyVal:

implicit class TIncrement[T : Increment](val underlying: T)  {
    def increment: T = implicitly[Increment[T]].increment(underlying)
}

But I am not sure I see the value in delegating to the type class here: rather than creating a type class implementation for every "incrementable" type, why not just have separate implicit classes that would just increment directly?

like

implicit class I(val v: Int) extends AnyVal { def increment = v + 1 }
implicit class L(val v: Long) extends AnyVal { def increment = v + 1 }
   // etc

UPDATE
Actually, you can do that with AnyVal too:

implicit class TIncrement[T](val underlying: T) extends AnyVal  {
    def increment(implicit inc: Increment[T]): T = inc.increment(underlying)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading