Scala - extension of reference type without subclass

I have a Java class that I would like to subclass. The subclass is to add only convenient methods - all this can be done using external methods, because I look only at public fields and do not change anything.

If the base class was a value type, I would use value wrappers - extends AnyVal . But the base class is a Java reference type. Is there a better way to subclass it besides extending it?

+5
source share
2 answers

To specifically indicate your second paragraph, the type that you carry with the value class can be a reference type, and you still avoid the extra distribution of objects that would normally be involved in a wrapper. For example, if you have these implicit classes:

 implicit class MyInt(val underlying: Int) extends AnyVal { def inc: Int = underlying + 1 } implicit class MyString(val underlying: String) extends AnyVal { def firstChar: Char = underlying.charAt(0) } implicit class MyNonValueClassString(val underlying: String) { def firstCharNonValueClass: Char = underlying.charAt(0) } 

And this code that uses them:

 println(42.inc) println("hello".firstChar) println("hello".firstCharNonValueClass) 

You can compile with -Xprint:flatten to see the canceled version (reformatted here for clarity):

 scala.this.Predef.println( scala.Int.box(Demo$MyInt.this.inc$extension(Demo.this.MyInt(42))) ); scala.this.Predef.println( scala.Char.box( Demo$MyString.this.firstChar$extension(Demo.this.MyString("hello")) ) ); scala.this.Predef.println( scala.Char.box( Demo.this.MyNonValueClassString("hello").firstCharNonValueClass() ) ); 

As you can see, calling firstChar does not include the new object.

+10
source

Enjoy composition over inheritance.

So, in your case, the best way is probably to create a class with a Java class as an attribute. Then you can simply delegate the methods of these objects and add your own:

 class MyClass(jo: JavaObject) { def delegateMethod() = jo.method() def newMethod() = // ... } 
+2
source

Source: https://habr.com/ru/post/1206653/


All Articles