0

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 ) ?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Helix112
  • 305
  • 3
  • 12
  • _"wanted y() to be accessible from this file only and undefined elsewhere"_ that should be the case unless you are importing it. - _"I could just have added a private class that inherits from class Person and thus add method y() in it."_ but then you require to use that private class instead of the original one. - _"other than the fact that this goes against the principle which implicit classes are essentially created for"_ Not sure what you mean. - _"what does really happen behind the scenes"_ it just creates a normal class and an implicit conversions. – Luis Miguel Mejía Suárez Sep 15 '21 at 15:25
  • 2
    _"does this have any performance issues"_ - I shouldn't, but just to be sure you may _(should)_ make that `implicit class` also a **value class** to ensure there is no instantiation. – Luis Miguel Mejía Suárez Sep 15 '21 at 15:26
  • @LuisMiguelMejíaSuárez , concerning my question about the principle stuff , from some googling I've read that it uses the pimp my library pattern , and thought that this feature should only be used for external libraries / APIs ' classes that we do not have access to , like primitive types for example. But am not sure if it is a bad practice to use implicit classes when there is no need to, like in enriching a pre-existent class of mine which i've created on my own. – Helix112 Sep 15 '21 at 16:47
  • 2
    Well, there is nothing wrong with it, the question would be if you control the class then why do you want an extension method at all instead of modifying the original class. But if you have valid reasons then go ahead. – Luis Miguel Mejía Suárez Sep 15 '21 at 16:50
  • @LuisMiguelMejíaSuárez , Also the scala [docs](https://docs.scala-lang.org/sips/implicit-classes.html") do point to another [link](https://jorgeortiz85.github.io/ImplicitClassSIP.xhtml) , in which it is stated that every invocation of the implicit conversion results in a creation of a new object , that's why I wanted to better understand this point of about performance stuff – Helix112 Sep 15 '21 at 16:52
  • 2
    In theory, it will, but in practice, the JIT should inline it. However, again, if you make it a **value class** _(i.e. `implicit class Foo(private val x : Person) extends AnyVal { ... }`)_ then the compiler will not generate the code for an object creation but rather just a method invocation. – Luis Miguel Mejía Suárez Sep 15 '21 at 16:55
  • 1
    1. https://stackoverflow.com/questions/14929422/should-implicit-classes-always-extend-anyval 2. http://www.michaelpollmeier.com/2020/01/14/scala-valueclass-allocations 3. https://failex.blogspot.com/2017/04/the-high-cost-of-anyval-subclasses.html – Dmytro Mitin Sep 28 '21 at 13:23

0 Answers0