the concept of subtyping explained from a type system
play

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


  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

  2. The Concept of Subtyping Explained from a Type System Perspective Agenda 1 Introduction 2 Subtyping 3 Subtyping in C# 4 Conclusion 2 / 27

  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

  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

  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

  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

  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

  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

  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

  10. The Concept of Subtyping Explained from a Type System Perspective Subtyping The World Without Subtyping Γ ⊢ t 1 : T 11 → T 12 Γ ⊢ t 2 : T 11 Γ ⊢ t 1 t 2 : T 12 Rule for function application 10 / 27

  11. The Concept of Subtyping Explained from a Type System Perspective Subtyping Typing Rules for Subtyping Γ ⊢ S < : T Subtype relation 11 / 27

  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

  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 override T e r r i e r Get () 12 { 13 . . . 14 } 15 } Listing 1: C# Return Type Covariance 13 / 27

  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

  15. The Concept of Subtyping Explained from a Type System Perspective Subtyping Typing Rules for Subtyping Γ ⊢ A 1 < : B 1 ... Γ ⊢ A n < : B n Γ ⊢ A n +1 ... Γ ⊢ A n + m Γ ⊢ Record ( l 1 : A 1 , ..., l n + m : A n + m ) < : Record ( l 1 : B 1 , ..., l n : B n ) Width subtyping 15 / 27

  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

  17. The Concept of Subtyping Explained from a Type System Perspective Subtyping Typing Rules for Subtyping Γ ⊢ A 1 < : B 1 ... Γ ⊢ A n < : B n Γ ⊢ Record ( l 1 : A 1 , ..., l n : A n ) < : Record ( l 1 : B 1 , ..., l n : B n ) Depth subtyping 17 / 27

  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

  19. The Concept of Subtyping Explained from a Type System Perspective Subtyping Typing Rules for Subtyping Γ ⊢ T 1 < : S 1 Γ ⊢ S 2 < : T 2 Γ ⊢ S 1 → S 2 < : T 1 → T 2 Subtype relation for function types 19 / 27

  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

  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

  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

  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 , or s u b c l a s s e s of 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

  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

  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

  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

  27. The Concept of Subtyping Explained from a Type System Perspective Conclusion Thank you for listening. Questions? 27 / 27

Recommend


More recommend