1 / 26

Combining Structural and Nominal Subtyping

Combining Structural and Nominal Subtyping. Donna Malayeri. Our goal: combine the benefits of structural and nominal subtyping. Structural subtyping a type T is a subtype of U if T’s methods are a superset of U’s methods

glen
Download Presentation

Combining Structural and Nominal Subtyping

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. Combining Structural and Nominal Subtyping Donna Malayeri

  2. Our goal: combine the benefits of structural and nominal subtyping Structural subtyping • a type T is a subtype of U if T’s methods are a superset of U’s methods • so, any class with a serialize() method would be a subtype of Serializable Nominal subtyping • a type T is a subtype of U only if T’s methods are a superset of U’s methods andT has been declared as a subtype of U

  3. Benefits of structural subtyping • Structural subtyping is flexible and compositional • allows unanticipated reuse • no unnecessary proliferation of types • useful for data persistence & distributed computing • Example: XML

  4. An example • java.util.Collection includes 15 methods • but 6 of these methods are marked “optional”:boolean add(E o) boolean addAll(Collection<? extends E> c) void clear() boolean remove(Object o) boolean removeAll(Collection<?> c) boolean retainAll(Collection<?> c) • A better solution: put these in a separate interface

  5. But this would lead to an explosion of types! ImmutableCollection MutableCollection ReadWriteCollection ImmutableAbstractList MutableAbstractList ReadWriteAbstractList ReadWriteArrayList .... and so on

  6. Structural subtyping would solve this problem • Don’t need to name all possible combinations of interesting methods • Don’t need to specify that a class implements a particular interface ahead of time • Easy to later define new subsets of methods that are interesting • e.g. Iterable = has method Iterator<E> iterator()

  7. More examples The classes org.eclipse.swt.widgets.Scrollbarandorg.eclipse.swt.widgets.Slider have many methods with the same Javadoc: boolean isEnabled() void setEnabled(boolean enabled) int getMinimum() int getMaximum() void setMinimum(int value) void setMaximum(int value) int getPageIncrement() void setPageIncrement(int value) int getSelection() void setSelection(int selection) int getThumb() void setThumb(int value) int getIncrement() void setIncrement(int value) void setValues(int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement)

  8. Other examples many classes have the methods String getText() void setText(String string) Button Label Link Item Text Combo Group ...but there’s no IText interface

  9. Other examples many classes have the methods void setImage(Image image) Image getImage() Button Caret Decorations Item Label MenuItem TabItem TableColumn TableItem TrayItem TreeColumn TreeItem ToolItem ...but there’s no IHasImage interface

  10. And, in the JDT all of these classes have String getElementName() IImportDeclaration IJavaElement ILocalVariable IMethod IPackageDeclaration IPackageFragment IType IField ...but there’s no IElement interface

  11. So, structural subtyping has definite practical uses

  12. Nominal subtyping has benefits too • allows programmers to explicitly express design intent • can prevent “accidental” subtyping • necessary for efficient run-time subtyping tests and external/multimethod dispatch • Examples • Java classes • ML datatypes & dispatch

  13. Nominal subtyping in Unity abstract brand Window (...)extends Top concrete brand Textbox (...) extends Window concrete brand StaticText (...)extends Window concrete brand ScrollingTextbox (...)extends Textbox

  14. Why do we care about external methods?

  15. What is an external method? • conceptually part of an existing class • performs dispatch on objects of that class’ type • doesn’t have to be in the same compilation unit as the class • closely related concept: multi-methods • method dispatch can depend on any subset of function’s arguments

  16. External methods • Suppose you want to be able to extend both classes and methods easily • Visitor doesn’t solve the problem; adding new classes is hard • Alternatives to external methods • manually do a typecase (e.g., “instanceof” tests) • make do with Visitor

  17. extensible types external methods External methods example abstract brand Window (...)extends Top concrete brand Textbox (...)extends Window concrete brand StaticText (...)extends Window fun paint (t:Textbox) = ... fun paint (t:StaticText) = ... new type + new method concrete brand ScrollingTextbox(...)extends Textbox fun paint ( t : ScrollingTextbox) = ...

  18. There’s lots of existing work on external methods • Cecil language specification • Millstein et al (TOPLAS ’04) • Clifton et al (TOPLAS ’06) • Lee and Chambers (ECOOP ’06) • I’m extending this work

  19. Structural subtyping in Unity val win = (title=“MyWindow”) win : (title:string) val textbox = (title=“MyTextbox”, text=“SSSG is fun”) textbox : (title:string, text:string) val scrollWin = (title=“ScrolllingWindow”, scroll=myScrollbar) scrollWin : (title:string, scroll:Scrollbar) ¿textbox·¿win (title:string, text:string)·(title:string) ¿scrollWin·¿win (title:string, scroll:Scrollbar) · (title:string)

  20. Nominal subtyping in Unity abstract brand Window (...)extends Top concrete brand Textbox (...) extends Window concrete brand StaticText (...)extends Window concrete brand ScrollingTextbox (...)extends Textbox

  21. ...combined with structural subtyping abstract brandWindow (title : string) extends Top concrete brandTextbox(title : string, currentPos : int) extendsWindow concrete brandStaticText(title : string, text : string) extendsWindow concrete brandScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

  22. ...combined with structural subtyping abstract brandWindow (title : string) extends Top concrete brandTextbox(title : string, currentPos : int) extendsWindow concrete brandStaticText(title : string, text : string) extendsWindow concrete brandScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

  23. ...combined with structural subtyping abstract brandWindow (title : string) extends Top concrete brandTextbox(title : string, currentPos : int) extendsWindow concrete brandStaticText(title : string, text : string) extendsWindow concrete brandScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox Subtyping relationships Window (title : string, s : Scrollbar)  Window (title : string) Textbox (...)  Window (title : string) ScrollingTextbox (...) Textbox (...) ScrollingTextbox (...) Window (title : string, s : Scrollbar) StaticText (...)  Window (title : string) StaticText (..., s : Scrollbar)  Window (title : string, s : Scrollbar)

  24. Writing functions fixed number of characters unlimited characters; scrolls if needed each branch is semantically an external method

  25. Comparison to Java  Unity Java 

  26. Summary • Unity supports structural and nominal subtyping in a unified type system • Future work: • extensible brands & external methods • polymorphism/row polymorphism Thank you

More Related