260 likes | 403 Views
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
E N D
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 • 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
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
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
But this would lead to an explosion of types! ImmutableCollection MutableCollection ReadWriteCollection ImmutableAbstractList MutableAbstractList ReadWriteAbstractList ReadWriteArrayList .... and so on
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()
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)
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
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
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
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
Nominal subtyping in Unity abstract brand Window (...)extends Top concrete brand Textbox (...) extends Window concrete brand StaticText (...)extends Window concrete brand ScrollingTextbox (...)extends Textbox
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
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
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) = ...
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
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)
Nominal subtyping in Unity abstract brand Window (...)extends Top concrete brand Textbox (...) extends Window concrete brand StaticText (...)extends Window concrete brand ScrollingTextbox (...)extends Textbox
...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
...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
...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)
Writing functions fixed number of characters unlimited characters; scrolls if needed each branch is semantically an external method
Comparison to Java Unity Java
Summary • Unity supports structural and nominal subtyping in a unified type system • Future work: • extensible brands & external methods • polymorphism/row polymorphism Thank you