|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Polymorphic Function Types" |
| 4 | +--- |
| 5 | + |
| 6 | +A polymorphic function type is a function type which accepts type parameters. |
| 7 | +For example: |
| 8 | +```scala |
| 9 | +// A polymorphic method: |
| 10 | +def foo[A](xs: List[A]): List[A] = xs.reverse |
| 11 | + |
| 12 | +// A polymorphic function value: |
| 13 | +val bar: [A] => List[A] => List[A] |
| 14 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 15 | +// a polymorphic function type |
| 16 | + = [A] => (xs: List[A]) => foo[A](xs) |
| 17 | +``` |
| 18 | +Scala already has _polymorphic methods_, i.e. methods which accepts type parameters. |
| 19 | +Method `foo` above is an example, accepting a type parameter `A`. |
| 20 | +So far, it |
| 21 | +was not possible to turn such methods into polymorphic function values like `bar` above, |
| 22 | +which can be passed as parameters to other functions, or returned as results. |
| 23 | + |
| 24 | +In Dotty this is now possible. The type of the `bar` value above is |
| 25 | + |
| 26 | +```scala |
| 27 | +[A] => List[A] => List[A] |
| 28 | +``` |
| 29 | + |
| 30 | +This type describes function values which take a type `A` as a parameter, |
| 31 | +then take a list of type `List[A]`, and return a list of the same type `List[A]`. |
| 32 | + |
| 33 | +[More details](https://github.com/lampepfl/dotty/pull/4672) |
| 34 | + |
| 35 | + |
| 36 | +### Example Usage |
| 37 | + |
| 38 | +Polymorphic function type are particularly useful |
| 39 | +when callers of a method are required to provide a |
| 40 | +function which has to be polymorphic, |
| 41 | +meaning that it should accept arbitrary types as part of its inputs. |
| 42 | + |
| 43 | +For instance, consider the situation where we have |
| 44 | +a data type to represent the expressions of a simple language |
| 45 | +(consisting only of variables and function application) |
| 46 | +in a strongly-typed way: |
| 47 | + |
| 48 | +```scala |
| 49 | +enum Expr[A]: |
| 50 | + case Var(name: String) |
| 51 | + case Apply[A, B](fun: Expr[B => A], arg: Expr[B]) extends Expr[A] |
| 52 | +``` |
| 53 | + |
| 54 | +We would like to provide a way for users to map a function |
| 55 | +over all immediate subexpressions of a given `Expr`. |
| 56 | +This requires the given function to be polymorphic, |
| 57 | +since each subexpression may have a different type. |
| 58 | +Here is how to implement this using polymorphic function types: |
| 59 | + |
| 60 | +```scala |
| 61 | +def mapSubexpressions[A](e: Expr[A])(f: [B] => Expr[B] => Expr[B]): Expr[A] = |
| 62 | + e match |
| 63 | + case Apply(fun, arg) => Apply(f(fun), f(arg)) |
| 64 | + case Var(n) => Var(n) |
| 65 | +``` |
| 66 | + |
| 67 | +And here is how to use this function to _wrap_ each subexpression |
| 68 | +in a given expression with a call to some `wrap` function, |
| 69 | +defined as a variable: |
| 70 | + |
| 71 | +```scala |
| 72 | +val e0 = Apply(Var("f"), Var("a")) |
| 73 | +val e1 = mapSubexpressions(e0)( |
| 74 | + [B] => (se: Expr[B]) => Apply(Var[B => B]("wrap"), se)) |
| 75 | +println(e1) // Apply(Apply(Var(wrap),Var(f)),Apply(Var(wrap),Var(a))) |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +### Relationship With Type Lambdas |
| 81 | + |
| 82 | +Polymorphic function types are not to be confused with |
| 83 | +[_type lambdas_](new-types/type-lambdas.md). |
| 84 | +While the former describes the _type_ of a polymorphic _value_, |
| 85 | +the latter is an actual function value _at the type level_. |
| 86 | + |
| 87 | +A good way of understanding the difference is to notice that |
| 88 | +**_type lambdas are applied in types, |
| 89 | +whereas polymorphic functions are applied in terms_**: |
| 90 | +One would call the function `bar` above |
| 91 | +by passing it a type argument `bar[Int]` _within a method body_. |
| 92 | +On the other hand, given a type lambda such as `type F = [A] =>> List[A]`, |
| 93 | +one would call `F` _withing a type expression_, as in `type Bar = F[Int]`. |
0 commit comments