I am trying to add new functions to existing types (so that I can have the IDE automatically suggest the appropriate functions for types that I do not control, such as Future[Option[A]] ). I studied both implicit classes and implicit conversions to accomplish this, and both of them seem to offer the same behavior.
Is there any effective difference between using an implicit class:
case class Foo(a: Int) implicit class EnrichedFoo(foo: Foo) { def beep = "boop" } Foo(1).beep
And using implicit conversion:
case class Foo(a: Int) trait Enriched { def beep: String } implicit def fooToEnriched(foo: Foo) = new Enriched { def beep = "boop" } Foo(1).beep
I believe that one difference here may be that the first example creates a one-time class instead of the attribute, but I could easily adapt the implicit class to extend the abstract attribute, for example:
case class Foo(a: Int) trait Enriched { def beep: String } implicit class EnrichedFoo(foo: Foo) extends Enriched { def beep = "boop" } Foo(1).beep
source share