Interface Related Improvements

Suminda Sirinath Salpitikorala Dharmasena sirinath1978m at gmail.com
Sun Nov 8 06:16:40 UTC 2020


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>;

*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>;
    }

*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() {}
    }

Groovy and Scala has ways to implement something similar with traits.

Suminda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20201108/e20b0b56/attachment.htm>


More information about the compiler-dev mailing list