[TYPES] Why cannot declare a super type dynamically?

Marco Servetto marco.servetto at gmail.com
Mon May 27 17:47:08 EDT 2019


On a more technical/programming note,
focusing on just Java interfaces:
not only could be theoretically safe to add interfaces later one,
someone told me a trick to actually force plain java to do it:
(however, I have never used this trick, if someone have practical
experience, please share!)
Java classes ad interfaces are loaded when they are used,
so is possible to write programs that generate bytecode for interface
I while running and before interface I is loaded.
So... when interfaces get loaded? not at the same time superclasses
get loaded, but much later if even.
(Java8 may have changed this somehow)
In particular they are loaded if needed for an instanceof test
Thus to have a class that have expandable supertypes you can do the following:

    interface ExpandableA0{}
    class A implements ExpandableA0{  .... }

Then, you can use A freely, just never use ExpandableA0, not even as a
type annotation.
Then, when you dynamicaly discover you want A to be subtype of
interface Foo, you do the following

    Late.implement(A.class,Foo.class)

internally this method will do something on the line of the following
pseudocode:

  implement(Class<?> c, Class<?> i){
    String expander="Expandable"+c.getName();//will be ExpandableA
    int num=classLoader.discoverSmalledUnloadedExpander(expander);
    classLoader.load(expander+num,"interface "+(expander+num)+"
extends "+(expander+(num+1))+", "+i.getName()+"{}")
    //at the first round it will be  "interface ExpandableA0 extends
ExpandableA1, Foo {}"
   }

Now, you can do

  A myA=new A();
  Late.implement(A.class,Foo.class)
  Foo f=(Foo)myA;

And the dynamic cast should be a success.

This also teach us abut formal soundness: OO type systems try to be
modular, this means that even if we do not know about all classes and
interfaces in the system, we can say that a part is correct in
"isolation"
It turns out interfaces do not always need to be considered to see if
a part implementing them is correct, if the corrisponding subtyping
assumptions are not used. Thus, we could say that our part is correct
parametric of what those interfaces can be, and this allows us to
dynamically assume subtyping

I may be wrong in some part of my reasoning/code, please fell free to
prove me wrong, it looks "to easy to be true"

On Mon, 27 May 2019 at 18:44, Xuhui Li <xhli06 at 163.com> wrote:
>
> [ The Types Forum, http://lists.seas.upenn.edu/mailman/listinfo/types-list ]
>
> Dear All,
>
>
> As far as I know, the existing type systems allow the subtyping being declared in the defintion of the sub types but forbid the super type being declared after the subtype . For example, class A extends B. However, if a new type C is declared and it is found that conceputually C should be a supertype of A (assume that the fields of C don't occur in A but an injection of the fields can be defined) , we cannot declare A extends C after A has been already defined. Why cannot this happen?  Will dynamically introducing a supertype threaten the safety of existing type system?
>
>
> Thanks for your attention.
>
>
> Best Regards
>
>
> Xuhui


More information about the Types-list mailing list