implicit classes in Scala : performance and good practice

242 Views Asked by At

As far as I know, implicit classes are used to provide extension methods to existing types , or let's say enrich types coming from other libraries or APIs. However, I was playing around and tried this :

case class Person(firstname : String , lastName:String ) {
  def x() = {
      println("x")
  }
}

implicit class Foo(x : Person) {
  def y() = {
    println("y")
  }
}

Person("","").y()

I wanted y() to be accessible from this file only and undefined elsewhere.

I could just have added a private class that inherit from class Person and thus add method y() in it. [1]

So my question is : other than the fact that this goes against the principle which implicit classes are essentially created for; what does really happen behind the scenes, and does this have any performance issues one should be aware of ( comparing to solution 1 ) ?

0

There are 0 best solutions below