Polymorphism Chapter Eight Modern Programming Languages 1 Introduction

  • Slides: 18
Download presentation
Polymorphism Chapter Eight Modern Programming Languages 1

Polymorphism Chapter Eight Modern Programming Languages 1

Introduction Compare these function types n The ML function is more flexible, since it

Introduction Compare these function types n The ML function is more flexible, since it can be applied to any pair of the same (equality-testable) type n Chapter Eight C: int f(char a, char b) { return a==b; } ML: - fun f(a, b) = (a = b); val f = fn : ''a * ''a -> bool Modern Programming Languages 2

Polymorphism Functions with that extra flexibility are called polymorphic n A difficult word to

Polymorphism Functions with that extra flexibility are called polymorphic n A difficult word to define: n – – – Chapter Eight Applies to a wide variety of language features Most languages have at least a little We will examine four major examples, then return to the problem of finding a definition that covers them Modern Programming Languages 3

Outline Overloading n Parameter coercion n Parametric polymorphism n Subtype polymorphism n Definitions and

Outline Overloading n Parameter coercion n Parametric polymorphism n Subtype polymorphism n Definitions and classifications n Chapter Eight Modern Programming Languages 4

Overloading An overloaded function name or operator is one that has at least two

Overloading An overloaded function name or operator is one that has at least two definitions, all of different types n Many languages have overloaded operators n Some also allow the programmer to define new overloaded function names and operators n Chapter Eight Modern Programming Languages 5

Predefined Overloaded Operators ML: Pascal: Chapter Eight val x = 1 + 2; val

Predefined Overloaded Operators ML: Pascal: Chapter Eight val x = 1 + 2; val y = 1. 0 + 2. 0; a b c d : = : = 1 + 2; 1. 0 + 2. 0; "hello " + "there"; ['a'. . 'd'] + ['f'] Modern Programming Languages 6

Adding to Overloaded Operators n Some languages, like C++, allow additional meanings to be

Adding to Overloaded Operators n Some languages, like C++, allow additional meanings to be defined for operators class complex { double rp, ip; // real part, imaginary part public: complex(double r, double i) {rp=r; ip=i; } friend complex operator+(complex, operator*(complex, complex); }; Chapter Eight void f(complex a, complex b, complex d = a + b * c; … } Modern Programming Languages complex c) { 7

Defining Overloaded Functions n Some languages, like C++, permit the programmer to overload function

Defining Overloaded Functions n Some languages, like C++, permit the programmer to overload function names int square(int x) { return x*x; } double square(double x) { return x*x; } Chapter Eight Modern Programming Languages 8

Implementing Overloading: Example C++: int shazam(int a, int b) {return a+b; } double shazam(double

Implementing Overloading: Example C++: int shazam(int a, int b) {return a+b; } double shazam(double a, double b) {return a+b; } Assembler: Chapter Eight shazam__Fii: lda $30, -32($30). frame $15, 32, $26, 0 … shazam__Fdd: lda $30, -32($30). frame $15, 32, $26, 0 … Modern Programming Languages 9

Outline Overloading n Parameter coercion n Parametric polymorphism n Subtype polymorphism n Definitions and

Outline Overloading n Parameter coercion n Parametric polymorphism n Subtype polymorphism n Definitions and classifications n Chapter Eight Modern Programming Languages 10

Coercion n A coercion is an implicit type conversion, supplied automatically even if the

Coercion n A coercion is an implicit type conversion, supplied automatically even if the programmer leaves it out double x; conversion in Java: x = (double) 2; Explicit type Coercion in Java: Chapter Eight double x; x = 2; Modern Programming Languages 11

Parameter Coercion n When a language supports coercion of parameters on a function call

Parameter Coercion n When a language supports coercion of parameters on a function call (or of operands when an operator is applied), the resulting function (or operator) is polymorphic Chapter Eight Modern Programming Languages 12

Example: Java void f(double x) { … } f((byte) 1); f((short) 2); f('a'); f(3);

Example: Java void f(double x) { … } f((byte) 1); f((short) 2); f('a'); f(3); f(4 L); f(5. 6 F); Chapter Eight This f can be called with any type of parameter Java is willing to coerce to type double Modern Programming Languages 13

Outline Overloading n Parameter coercion n Parametric polymorphism n Subtype polymorphism n Definitions and

Outline Overloading n Parameter coercion n Parametric polymorphism n Subtype polymorphism n Definitions and classifications n Chapter Eight Modern Programming Languages 14

Parametric Polymorphism A function exhibits parametric polymorphism if it has a type that contains

Parametric Polymorphism A function exhibits parametric polymorphism if it has a type that contains one or more type variables n A type with type variables is a polytype n Found in languages including ML, C++ and Ada n Chapter Eight Modern Programming Languages 15

Example: C++ Function Templates template<class X> X max(X a, X b) { return a>b

Example: C++ Function Templates template<class X> X max(X a, X b) { return a>b ? a : b; } void g(int a, int b, char c, char d) { int m 1 = max(a, b); char m 2 = max(c, d); } Note that > can be overloaded, so X is not limited to types for which > is predefined. Chapter Eight Modern Programming Languages 16

Example: ML Functions - fun identity x = x; val identity = fn :

Example: ML Functions - fun identity x = x; val identity = fn : 'a -> 'a - identity 3; val it = 3 : int - identity "hello"; val it = "hello" : string - fun reverse x = = if null x then nil = else (reverse (tl x)) @ [(hd x)]; val reverse = fn : 'a list -> 'a list Chapter Eight Modern Programming Languages 17

Conclusion n We have seen polymorphic functions A function or operator is polymorphic if

Conclusion n We have seen polymorphic functions A function or operator is polymorphic if it has at least two possible types Overloading – n Each different type requires a separate definition Parameter coercion – Chapter Eight different types can be coerced to a given parameter type Modern Programming Languages 18