Interface Related Improvements
Remi Forax
forax at univ-mlv.fr
Sun Nov 8 11:00:32 UTC 2020
It has been already proposed several times under the name "properties for Java", see [1] by example.
more comments inlined below.
[1] https://mail.openjdk.java.net/pipermail/amber-spec-comments/2020-September/000035.html
----- Mail original -----
> De: "Suminda Sirinath Salpitikorala Dharmasena" <sirinath1978m at gmail.com>
> À: "discuss" <discuss at openjdk.java.net>, "compiler-dev" <compiler-dev at openjdk.java.net>, "Amber Expert Group Observers"
> <amber-spec-observers at openjdk.java.net>, "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Dimanche 8 Novembre 2020 07:16:40
> Objet: Interface Related Improvements
> Hello,
>
> I am not sure where to post this, hope some one interested picks this up
> for consideration.
>
> With the introduction of default methods, private methods and static
> methods in interfaces a few more changes are desirable to improve usability.
>
> *Private fields*
>
> Since interfaces currently can have private methods it would be much
> desirable to have private fields also. These can static or instance fields,
> but minimally at least private static fields initially.
>
> If they are instance fields they will be only initialised once regardless
> of how many places the interface is extended.
>
> *Initializers*
>
> Interfaces can add initialisers. These will only run one. Minimally at
> least static initialisers initially.
>
> *Reuse of Default Interface Methods*
>
> Default methods can improve code reuse in anonymous inner classes and enums.
>
> *Improvements to Annonymous Inner Classes to work with the new Java
> Interfaces*
>
> In order to reuse default methods, it will be convenient if something like
> the following can be valid code. This is similar to what Scala and Groovy
> does with traits.
>
> class X {}
>
> interface P<T> {
> default void f() {}
> }
>
> ...
>
> X x = new X() implements P<T>;
Use a local class (a class inside a method) instead
...
class XImpl extends X implements P<T> { }
X x = new Ximpl();
>
> *Improving Enums to work with the new Java Interfaces*
>
> In Enums there are times one does not need to share all
> implementations like in:
>
> enum E {
> X {
> f() {}
> },
> Y {
> g() {}
> };
> }
>
> In such cases some times the implementation of f and g can be shared if
> following is allowed:
>
> interface P<X> {
> default void f() {}
> }
>
> interface Q<Y> {
> default void g() {}
> }
>
> enum E {
> X impliments P<X>,
> Y impliments Q<Y>;
> }
Implementing an interface with a default method is not the simplest way to share code, calling a static method of another class is.
If you implement an interface, you get also subtyping that you don't use here given that the enum fields are typed with the type of the enum.
In a way, you are trying to use interface implementation where you can just use delegation.
>
> *Protected Methods*
>
> Protected methods could be considered to control what can be accessed on interfaces.
>
> *Allow this*
>
> Make the meaning of this pointer special in interfaces so features in the
> implementing classes can be accessed:
>
> interface P {
> X this;
>
> default void f() {
> x();
> }
> }
>
> interface Q {
> X this;
>
> default void g() {
> x();
> }
> }
>
> class X implements P, Q {
> void x() {}
> }
>
Declaring an abstract method "void x();" in the interface give you the same semantics without inventing a new syntax.
> Groovy and Scala has ways to implement something similar with traits.
>
> Suminda
cheers,
Rémi
More information about the amber-dev
mailing list