Combining Structural and Nominal Subtyping

Preview:

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Combining Structural and Nominal Subtyping

Donna Malayeri

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

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

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

3/5/07 5

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

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()

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)

3/5/07 8

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

ButtonLabelLinkItemTextComboGroup ...but there’s no IText interface

3/5/07 9

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

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

3/5/07 10

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

IImportDeclarationIJavaElementILocalVariableIMethodIPackageDeclarationIPackageFragmentITypeIField

...but there’s no IElement interface

3/5/07 11

So, structural subtyping has definite practical uses

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

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

3/5/07 14

Why do we care about external methods?

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

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

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

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

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)

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

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

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

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

3/5/07 24

Writing functions

fixed number of characters

unlimited characters; scrolls if needed

each branch is semantically an external method

3/5/07 25

Comparison to Java

Unity

Java

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

Recommended