Polymorphic Symmetric Multiple Dispatch with Variance Gyunghee Park, Jaemin Hong, Guy L. Steele Jr., Sukyoung Ryu POPL '19
Method Overloading add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … � 2 / 10
Method Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) � 3 / 10
Method Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) 3 / 10
Static Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) 4 / 10
Single Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) 5 / 10
Single Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) 5 / 10
Binary Method Problem 6 / 10 Odersky et al., Programmin in Scala (3rd Ed.) , Ch. 30 Object Equality
Symmetric Multiple Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) 7 / 10
Symmetric Multiple Dispatch add(x: Int, y: Int): Int = … add(x: List, y: List): List = … add(x: SortedList, y: List): SortedList = … add(x: SortedList, y: SortedList): SortedList = … a: List = SortedList(1, 3, 5) List b: List = SortedList(2, 4, 6) SortedList add(a, b) 7 / 10
Unambiguity add(x: List, y: SortedList): List = … add(x: SortedList, y: List): List = … List a: List = SortedList(1, 3, 5) b: SortedList = SortedList(2, 4, 6) SortedList add(a, b) 8 / 10
Unambiguity add(x: List, y: SortedList): List = … add(x: SortedList, y: List): List = … List a: List = SortedList(1, 3, 5) b: SortedList = SortedList(2, 4, 6) SortedList add(a, b) 8 / 10
Type Preservation add(x: List, y: SortedList): SortedList = … add(x: SortedList, y: SortedList): List = … List a: List = SortedList(1, 3, 5) b: SortedList = SortedList(2, 4, 6) SortedList add(a, b) 9 / 10
Type Preservation add(x: List, y: SortedList): SortedList = … add(x: SortedList, y: SortedList): List = … List a: List = SortedList(1, 3, 5) b: SortedList = SortedList(2, 4, 6) SortedList add(a, b): SortedList 9 / 10
Overloading Rules 10 / 10
Overloading Rules 10 / 10
Recommend
More recommend