The Concept of Subtyping Explained from a Type System Perspective - - PowerPoint PPT Presentation

the concept of subtyping explained from a type system
SMART_READER_LITE
LIVE PREVIEW

The Concept of Subtyping Explained from a Type System Perspective - - PowerPoint PPT Presentation

The Concept of Subtyping Explained from a Type System Perspective The Concept of Subtyping Explained from a Type System Perspective Andr e Gasser HSR Hochschule f ur Technik Rapperswil June 10, 2015 1 / 27 The Concept of Subtyping


slide-1
SLIDE 1

The Concept of Subtyping Explained from a Type System Perspective

The Concept of Subtyping Explained from a Type System Perspective

Andr´ e Gasser

HSR Hochschule f¨ ur Technik Rapperswil

June 10, 2015

1 / 27

slide-2
SLIDE 2

The Concept of Subtyping Explained from a Type System Perspective

Agenda

1 Introduction 2 Subtyping 3 Subtyping in C# 4 Conclusion

2 / 27

slide-3
SLIDE 3

The Concept of Subtyping Explained from a Type System Perspective Introduction

Agenda

1 Introduction 2 Subtyping 3 Subtyping in C# 4 Conclusion

3 / 27

slide-4
SLIDE 4

The Concept of Subtyping Explained from a Type System Perspective Introduction

Type Systems

Integral part of modern programming languages They help us finding bugs early Typing rules are an essential part Subtyping is a feature of the type system

4 / 27

slide-5
SLIDE 5

The Concept of Subtyping Explained from a Type System Perspective Introduction

The Need for a Type System

Detecting errors early Abstractions Documentation Language safety Efficiency

5 / 27

slide-6
SLIDE 6

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Agenda

1 Introduction 2 Subtyping 3 Subtyping in C# 4 Conclusion

6 / 27

slide-7
SLIDE 7

The Concept of Subtyping Explained from a Type System Perspective Subtyping

What is a Subtype?

Notion of a Subtype ”The intuitive idea of a subtype is one whose objects provide all the behavior of objects of another type (the supertype) plus something extra. ...” -Barbara Liskov

7 / 27

slide-8
SLIDE 8

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Subtyping vs. Subclassing

How to differentiate ”In short, subtyping is a mathematical concept having to do with substitutability, whereas subclassing is a type construction method found in most object-oriented languages (if not all).”

  • Ward Cunningham

Subtyping The mathematical concept Subtypes can be safely substituted by their subtypes Subclassing (inheritance) Is one way to create new subtypes (alternative: structural subtyping, aka duck typing)

8 / 27

slide-9
SLIDE 9

The Concept of Subtyping Explained from a Type System Perspective Subtyping

The Need for Subtyping

Decrease Coupling of Code Supports incremental design Related types (side note: type hierarchies) Organizing a type library

9 / 27

slide-10
SLIDE 10

The Concept of Subtyping Explained from a Type System Perspective Subtyping

The World Without Subtyping

Γ ⊢ t1 : T11 → T12 Γ ⊢ t2 : T11 Γ ⊢ t1t2 : T12

Rule for function application

10 / 27

slide-11
SLIDE 11

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Typing Rules for Subtyping

Γ ⊢ S <: T

Subtype relation

11 / 27

slide-12
SLIDE 12

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Covariance and Contravariance

Use a less derived type in method parameters Use a more derived type in return value Principle of Safe Substitution

12 / 27

slide-13
SLIDE 13

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Example: Return Type Covariance

1 c l a s s Dog { } 2 c l a s s T e r r i e r : Dog { } 3 4 abstract c l a s s Enclosure 5 { 6 public abstract Dog Get ( ) ; 7 } 8 9 c l a s s DogHouse : Enclosure 10 { 11 public

  • verride

T e r r i e r Get () 12 { 13 . . . 14 } 15 }

Listing 1: C# Return Type Covariance

13 / 27

slide-14
SLIDE 14

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Typing Rules for Subtyping

Γ ⊢ s : S Γ ⊢ S <: T Γ ⊢ s : T

Subsumption rule

14 / 27

slide-15
SLIDE 15

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Typing Rules for Subtyping

Γ ⊢ A1 <: B1...Γ ⊢ An <: Bn Γ ⊢ An+1...Γ ⊢ An+m Γ ⊢ Record(l1 : A1, ..., ln+m : An+m) <: Record(l1 : B1, ..., ln : Bn)

Width subtyping

15 / 27

slide-16
SLIDE 16

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Example: Width Subtyping

1 Point { x : int , y : i n t } 2 Point3D { x : int , y : int , z : i n t }

Listing 2: Width Subtyping for Record Types

16 / 27

slide-17
SLIDE 17

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Typing Rules for Subtyping

Γ ⊢ A1 <: B1...Γ ⊢ An <: Bn Γ ⊢ Record(l1 : A1, ..., ln : An) <: Record(l1 : B1, ..., ln : Bn)

Depth subtyping

17 / 27

slide-18
SLIDE 18

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Example: Depth Subtyping

1 c l a s s Fish { } 2 c l a s s GoldFish : Fish { } 3 4 Aquarium 5 { 6 volume : int , 7 f i s h : Fish 8 } 9 10 GoldFishAquarium 11 { 12 volume : int , 13 f i s h : GoldFish 14 }

Listing 3: Depth Subtyping for Record Types

18 / 27

slide-19
SLIDE 19

The Concept of Subtyping Explained from a Type System Perspective Subtyping

Typing Rules for Subtyping

Γ ⊢ T1 <: S1 Γ ⊢ S2 <: T2 Γ ⊢ S1 → S2 <: T1 → T2

Subtype relation for function types

19 / 27

slide-20
SLIDE 20

The Concept of Subtyping Explained from a Type System Perspective Subtyping in C#

Agenda

1 Introduction 2 Subtyping 3 Subtyping in C# 4 Conclusion

20 / 27

slide-21
SLIDE 21

The Concept of Subtyping Explained from a Type System Perspective Subtyping in C#

State of Play

Support for subtyping in C# (my findings): Covariance in Arrays: Yes, but broken by design Variance in Methods: Not Supported Variance in Delegates: Supported Width Subtyping: Not Supported Depth Subtyping: Not Supported

21 / 27

slide-22
SLIDE 22

The Concept of Subtyping Explained from a Type System Perspective Subtyping in C#

Covariance in Arrays

Array Covariance - Broken by Design ”This is my candidate for worst feature of C#. It allows assignments to fail at runtime without any indication in the source code that such failure is possible. It imposes a performance cost on extremely common code to make a rare scenario go quickly; accessing an array of unsealed reference type safely happens much more often than covariant array conversions do. I much prefer the type-safe covariance that has been added to IEnumerable<T>.”

  • Eric Lippert

22 / 27

slide-23
SLIDE 23

The Concept of Subtyping Explained from a Type System Perspective Subtyping in C#

Covariance in Arrays

1 c l a s s Dog { } 2 c l a s s T e r r i e r : Dog { } 3 c l a s s B o r d e r C o l l i e : Dog { } 4 5 // Dog [ ] can contain 6 // Dogs ,

  • r

s u b c l a s s e s

  • f

Dogs 7 Dog [ ] dogs = new Dog [ 3 ] ; 8 dogs [ 0 ] = new Dog( ” L a s s i e ” ) ; 9 dogs [ 1 ] = new T e r r i e r ( ” Jack ” ) ; 10 dogs [ 2 ] = new B o r d e r C o l l i e ( ” Kirby ” ) ;

Listing 4: Array Type Covariance

23 / 27

slide-24
SLIDE 24

The Concept of Subtyping Explained from a Type System Perspective Subtyping in C#

Covariance in Arrays

1 c l a s s Dog { } 2 c l a s s T e r r i e r : Dog { } 3 c l a s s B o r d e r C o l l i e : Dog { } 4 5 Dog [ ] dogs = new T e r r i e r [ 3 ] ; 6 7 // This i s OK 8 dogs [ 0 ] = new T e r r i e r ( ” Jack ” ) ; 9 10 // This l e a d s to a runtime e r r o r 11 dogs [ 1 ] = new B o r d e r C o l l i e ( ” Kirby ” ) ;

Listing 5: Array Covariance

24 / 27

slide-25
SLIDE 25

The Concept of Subtyping Explained from a Type System Perspective Conclusion

Agenda

1 Introduction 2 Subtyping 3 Subtyping in C# 4 Conclusion

25 / 27

slide-26
SLIDE 26

The Concept of Subtyping Explained from a Type System Perspective Conclusion

Conclusion

Type systems are an essential part of modern programming languages Subtyping assists the software engineer in various ways C# only implements a subset of the functionality as it is described in type theory What goes into a type system seems to be the results of a diplomatic process

26 / 27

slide-27
SLIDE 27

The Concept of Subtyping Explained from a Type System Perspective Conclusion

Thank you for listening. Questions?

27 / 27