0

I need to create some extension methods in my Java code. I've read some posts here in SO and people suggest XTend or Scala in order to achieve this.

Now, my question would be.. if i write kind of an Adapter layer in Scala (adding there my extension methods) and then using that project as a dependency for my own Java project, are those extended methods available for me to use, or they are defined just for the 'scope of Scala project' and then the JVM output cannot provide those new methods to the other project using it?

EDIT: What i need to do is to extend a full hierarchy of classes in a given library and give some new functionality. As for Java's first approach I should extend every class in that hierarchy creating my own hierarchy of extended classes adding the new method there. I would like to avoid this and give the final user the sense of native functionality in the original hierarchy.

Regards.

  • 2
    C# extension methods are just syntactic sugar for static methods that take an instance of the extended type as the first argument. – Matias Cicero Aug 29 '16 at 18:10
  • 1
    Lets put it that way: what exactly is the problem that you intend to solve using this (complicated?) approach? – GhostCat Aug 29 '16 at 18:15
  • I'm not completly agree with the "syntactic sugar" thing. It gives final user a really more semantic usage of the added functionality, but ok, i'm not gonna argue about that hehe. I've edited the original post to give some background of my scenario. – Lautaro Rodriguez Garrido Aug 29 '16 at 18:23
  • [Java equivalent to C# extension methods](http://stackoverflow.com/questions/4359979/java-equivalent-to-c-sharp-extension-methods) – sstan Aug 29 '16 at 18:58
  • Been there @sstan. It's a feasible approach but I'm asking for one specific implementation in Scala or XTend to know if it can be done. If not, I'll probly go with regular static methods w/o the semantic win of extension methods. – Lautaro Rodriguez Garrido Aug 29 '16 at 19:02
  • 1
    Scala has a mechanism for extension methods, but that works on the compiler level so that will be pretty useless in your Java code. – Jasper-M Aug 29 '16 at 20:00
  • @Jasper-M do you know if the IDE can handle this in some way if I create a project with mixed Scala & Java, lets say in Eclipse? – Lautaro Rodriguez Garrido Aug 29 '16 at 20:09
  • You can have a mixed Java/Scala project, but I think scalac just passes the Java code on to javac after a bit of parsing to get some basic type information out of it. So no, that will not work. – Jasper-M Aug 29 '16 at 20:18
  • Ok, so probly I'll need to think about writing the whole code in Scala or just using static methods for it. Thanks :) – Lautaro Rodriguez Garrido Aug 29 '16 at 20:21

1 Answers1

0

As mentioned above in the comments, it is very close to C# but not exactly there because of the type erasure. For example, this works fine:

object myLibExtensions {
    implicit class TypeXExtension( val obj: TypeX ) extends AnyRef {
        def myCustomFunction( a: String ): String = {
            obj.someMethod(a)
        }
    }
}

It will act somewhat similar to C# extension methods, i.e. create static method wrappers in reasonable cases (but not always).

The only thing I am missing in Scala is that you can't (or at least I couldn't figure out how to) return the values of the types being extended. For example, assume I want to have something like an extension method "withMeta" that works as follows:

class TypeY extends TypeX { def methodOfY(...) ...}

var y: TypeY = ....

y.withMeta(...).methodOfY(...)

The following didn't work for me:

object myLibExtensions {
    private val something = ....
    implicit class Extension[T<:TypeX]( val obj: T ) extends AnyRef {
        def withMeta( meta: Meta[T] ): T = {
            something.associateMeta(obj,meta)
            val
        }
    }
} 

... because T is being erased to TypeX. So effectively you will have to write extensions for all specific leaf classes of the hierarchy in this case, which is sad.

Borv
  • 623
  • 5
  • 14