1 / 13

Virtual Types

Virtual Types. by Kresten Krab Thorup & by Mads Torgersen. Outline. OO Type System Limitation Examples Type safety MyType, ThisType, @T. OO Type System Limitation. Methods can be virtual But not inner classes No change to parameter when overriding Or only covariant

phiala
Télécharger la présentation

Virtual Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Virtual Types by Kresten Krab Thorup& by Mads Torgersen

  2. Outline • OO Type System Limitation • Examples • Type safety • MyType, ThisType, @T

  3. OO Type System Limitation • Methods can be virtual • But not inner classes • No change to parameter when overriding • Or only covariant • No change to the type of instance variable

  4. Example (Orig. OO) • interface Observer{ void notify(Subject s, Object e);} • class Subject{ Observer [] observers; void notifyObservers(Object e){ foreach(Observer ob : observers) ob.notify(this, e);}

  5. Discussion (Orig. OO) • Specialize subject? • Specialize event? • Runtime type-casting

  6. Example (VirtualTypes, Observer) • Interface Observer{ typedef SType as Subject; typedef EventType as Object; void notify(SType s, EventType e);}

  7. Example (VirtualTypes, Subject) • class Subject{ typedef OType as Observer; typedef EventType as Object; OType [] observers; notifyObservers(EventType e){ foreach(OType ob : observers) ob.notify(this, e);}

  8. Example (VirtualTypes, Specialize) • interface WindowObserver extends Observer{ typedef SType as WindowSubject; typedef EventType as WindowEvent;} • Class WindowSubject extends Subject{ typedef OType as WindowObserver; typedef EventType as WindowEvent;…}

  9. Discussion (VirtualTypes) • Strongly typed, specialized subject and event • But parameter type narrowing! • class Subject: notifyObservers(EventType e) -> notifyObservers(Object e) • class WindowSubject:-> notifyObservers(WindowEvent e) • Subject subj = new WindowSubject();subj.notifyObserver(new Object());// Error!!

  10. Type Safety • Problematic statements: • Subject subj = new WindowSubject();subj.notifyObserver(new Object()); • Correction: • Subj.notifyObserver( new subj.EventType())

  11. Type Safety (cont.) • τi should be a subtype of the virtual type variable Ti in τ’ instead of the concrete type assigned to Ti. • <τ’, Ti> v.s. τ’.Ti • subj.EventType v.s. Object

  12. @T • “Exact type” • class B extends A{} • A a = new B(); • @A aExact = new A(); • @A aExact = new B(); // Error • A a = new B();@A aExact = a; // Error • @A aExact = (@A) a; // Runtime check

  13. ThisType • interface ListIfc{ public char head(); public @ThisType tail(); public void setHead(char h); public void setTail(@ThisType t);} • class List implements @ListIfc{ protected char h; protected @ThisType t;…}

More Related