Transcript
Page 1: Combining Structural and Nominal Subtyping

Combining Structural and Nominal Subtyping

Donna Malayeri

Page 2: Combining Structural and Nominal Subtyping

3/5/07 2

Our goal: combine the benefits of structural and nominal subtypingStructural 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 and T has been declared as a subtype of U

Page 3: Combining Structural and Nominal Subtyping

3/5/07 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

Page 4: Combining Structural and Nominal Subtyping

3/5/07 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

Page 5: Combining Structural and Nominal Subtyping

3/5/07 5

But this would lead to an explosion of types!ImmutableCollectionMutableCollectionReadWriteCollectionImmutableAbstractListMutableAbstractListReadWriteAbstractListReadWriteArrayList.... and so on

Page 6: Combining Structural and Nominal Subtyping

3/5/07 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()

Page 7: Combining Structural and Nominal Subtyping

3/5/07 7

More examplesThe classes org.eclipse.swt.widgets.Scrollbar andorg.eclipse.swt.widgets.Sliderhave 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)

Page 8: Combining Structural and Nominal Subtyping

3/5/07 8

Other examplesmany classes have the methodsString getText() void setText(String string)

ButtonLabelLinkItemTextComboGroup ...but there’s no IText interface

Page 9: Combining Structural and Nominal Subtyping

3/5/07 9

Other examplesmany classes have the methodsvoid setImage(Image image)Image getImage()

ButtonCaretDecorationsItemLabelMenuItemTabItemTableColumnTableItemTrayItemTreeColumn TreeItemToolItem ...but there’s no IHasImage interface

Page 10: Combining Structural and Nominal Subtyping

3/5/07 10

And, in the JDTall of these classes haveString getElementName()

IImportDeclarationIJavaElementILocalVariableIMethodIPackageDeclarationIPackageFragmentITypeIField

...but there’s no IElement interface

Page 11: Combining Structural and Nominal Subtyping

3/5/07 11

So, structural subtyping has definite practical uses

Page 12: Combining Structural and Nominal Subtyping

3/5/07 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

Page 13: Combining Structural and Nominal Subtyping

3/5/07 13

Nominal subtyping in Unity

abstract brand Window (...) extends Topconcrete brand Textbox (...) extends Windowconcrete brand StaticText (...) extends Windowconcrete brand ScrollingTextbox (...) extends Textbox

Page 14: Combining Structural and Nominal Subtyping

3/5/07 14

Why do we care about external methods?

Page 15: Combining Structural and Nominal Subtyping

3/5/07 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

Page 16: Combining Structural and Nominal Subtyping

3/5/07 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

Page 17: Combining Structural and Nominal Subtyping

3/5/07 17

External methods example

abstract brand Window (...) extends Topconcrete brand Textbox (...) extends Windowconcrete brand StaticText (...) extends Window

fun paint (t : Textbox) = ...fun paint (t : StaticText) = ...

concrete brand ScrollingTextbox (...) extends Textbox

fun paint ( t : ScrollingTextbox) = ...

extensible types

new type + new method

external methods

Page 18: Combining Structural and Nominal Subtyping

3/5/07 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

Page 19: Combining Structural and Nominal Subtyping

3/5/07 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)

¿scrollWin · ¿win

(title:string, scroll:Scrollbar) · (title:string)

¿textbox · ¿win

(title:string, text:string) · (title:string)

Page 20: Combining Structural and Nominal Subtyping

3/5/07 20

Nominal subtyping in Unity

abstract brand Window (...) extends Topconcrete brand Textbox (...) extends Windowconcrete brand StaticText (...) extends Windowconcrete brand ScrollingTextbox (...) extends Textbox

Page 21: Combining Structural and Nominal Subtyping

3/5/07 21

...combined with structural subtyping

abstract brand Window (title : string) extends Topconcrete brand Textbox(title : string, currentPos : int) extends Windowconcrete brand StaticText(title : string, text : string) extends Windowconcrete brand ScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

Page 22: Combining Structural and Nominal Subtyping

3/5/07 22

...combined with structural subtyping

abstract brand Window (title : string) extends Topconcrete brand Textbox(title : string, currentPos : int) extends Windowconcrete brand StaticText(title : string, text : string) extends Windowconcrete brand ScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

Page 23: Combining Structural and Nominal Subtyping

3/5/07 23

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)

...combined with structural subtyping

abstract brand Window (title : string) extends Topconcrete brand Textbox(title : string, currentPos : int) extends Windowconcrete brand StaticText(title : string, text : string) extends Windowconcrete brand ScrollingTextbox (title : string, currentPos : int, s : Scrollbar) extends Textbox

Page 24: Combining Structural and Nominal Subtyping

3/5/07 24

Writing functions

fixed number of characters

unlimited characters; scrolls if needed

each branch is semantically an external method

Page 25: Combining Structural and Nominal Subtyping

3/5/07 25

Comparison to Java

Unity

Java

Page 26: Combining Structural and Nominal Subtyping

3/5/07 26

Summary

Unity supports structural and nominal subtyping in a unified type system

Future work: extensible brands & external methods polymorphism/row polymorphism

Thank you


Top Related