Groovy extension module compared to java inheritance

Does the groovy extension module have a hybrid form of java inheritance function? Why should an extension be declared static?

+6
source share
3 answers

The short answer is yes, I think. It is a little difficult to answer clearly, since the inheritance of extension methods is completely performed by the runtime (and the static compiler). So this has nothing to do with how Java inherits.

To answer the second question ... They are static, because for situations in which you need a state, you usually use a meta class. Extension methods are initially considered convenient methods or make the API more Groovy. Thus, they are a special form of methods added to the meta class. You can see them as a simplified version. But it also means that they do not have all the abilities. Implementing extension methods that can support local state based on a "self" object (basically which fields / properties will do) is actually hard to make effective ... but you can always use meta classes for each class to do this.

0
source

For all extensive purposes, they are syntactic sugar, so the static method seems more like OOP. There is no inheritance because static methods in Java and Groovy do not participate in inheritance (that is, classes do not inherit static methods).

Methods must be static because the compiler does not know how to instantiate the surrounding extension method class.

However, I believe that there are languages โ€‹โ€‹that allow you to define methods outside the surrounding class and perform some kind of inheritance, but not many of them (I believe that CLOS and Dylan do). In addition, they represent many languages โ€‹โ€‹that seem to allow methods to be added, but the type of โ€œobjectโ€ is actually changed / hidden for some other type. This is called adhock polymorphism (e.g. Clojure, Haskell, like Golang and a kind of Scala), but again has nothing to do with inclusive polymorphism (Java inheritance).

0
source

Unfortunately, reference documentation and other documents do not define the semantics of extension methods:

  • Q. Can they override instance methods?
    I tested extension methods using the use Category methods and metaClass methods. None of the approaches override instance methods. I did not test extension modules installed through the module descriptor .
  • Q. Can they be overridden by extension methods on subclasses?
    I also experienced it. The use methods and metaClass extension methods metaClass not overridden by extension methods in subclasses.
  • Q. Can they call inherited methods super ?
    No, because they are implemented using static methods.
  • Q. Can they call private methods?
    The experiments showed that they can, surprisingly.
  • Q. Can they access private instance variables?
    No, because they are implemented using static methods.
  • Q. Are they callable from Java methods?
    It is possible if the extension module is on the way to the class when compiling the calling code. I have not tested it.

Conclusion:. Extension methods are not a form of inheritance. They seem like a dynamic form of Universal Function Call Syntax (UFCS) , that is, when the language cannot find the variable.foo(arguments) method, it looks for the static extension function foo(variable, arguments) to call. [Please correct my hypothesis, if not!]

You asked why they are defined as static . This is similar to semantics: a static function that does not participate in inheritance, although its call syntax makes it look like a convenient method call.

You can write an extension method, such as an instance method, using the @ groovy.lang.Category annotation. It does AST transformations at compile time to turn it into a suitable static method.

Also see Groovy features . This is the form (mixin) of inheritance.

0
source

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


All Articles