Skip to content

Commit 9441553

Browse files
committed
Document polymorphic function types
1 parent 8201bc8 commit 9441553

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

docs/docs/reference/new-types/dependent-function-types.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ layout: doc-page
33
title: "Dependent Function Types"
44
---
55

6-
A dependent function type describes functions where the result type may depend
7-
on the function's parameter values. Example:
6+
A dependent function type is a function type whose result depends
7+
on the function's parameters. For example:
88
```scala
99
trait Entry { type Key; val key: Key }
1010

1111
def extractKey(e: Entry): e.Key = e.key // a dependent method
12+
1213
val extractor: (e: Entry) => e.Key = extractKey // a dependent function value
13-
// ║ ⇓ ⇓ ⇓ ⇓ ⇓ ⇓ ⇓ ║
14-
// ║ Dependent ║
15-
// ║ Function Type ║
16-
// ╚═══════════════════╝
14+
// ^^^^^^^^^^^^^^^^^^^
15+
// a dependent function type
1716
```
1817
Scala already has _dependent methods_, i.e. methods where the result
1918
type refers to some of the parameters of the method. Method
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
### Relationship With Type Lambdas
36+
37+
Polymorphic function types are not to be confused with
38+
[_type lambdas_](new-types/type-lambdas.md).
39+
While the former describes the _type_ of a polymorphic _value_,
40+
the latter is an actual function value _at the type level_.
41+
42+
A good way of understanding the difference is to notice that
43+
**_type lambdas are applied in types,
44+
whereas polymorphic functions are applied in terms_**:
45+
One would call the function `bar` above
46+
by passing it a type argument `bar[Int]` _within a method body_.
47+
On the other hand, given a type lambda such as `type F = [A] =>> List[A]`,
48+
one would call `F` _withing a type expression_, as in `type Bar = F[Int]`.

docs/docs/reference/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ These are additions to the language that make it more powerful or pleasant to us
107107
- [Enums](enums/enums.md) provide concise syntax for enumerations and [algebraic data types](enums/adts.md).
108108
- [Parameter Untupling](other-new-features/parameter-untupling.md) avoids having to use `case` for tupled parameter destructuring.
109109
- [Dependent Function Types](new-types/dependent-function-types.md) generalize dependent methods to dependent function values and types.
110-
- [Polymorphic Function Types](https://github.com/lampepfl/dotty/pull/4672) generalize polymorphic methods to dependent function values and types. _Current status_: There is a proposal, and a prototype implementation, but the implementation has not been finalized or merged yet.
110+
- [Polymorphic Function Types](new-types/polymorphic-function-types.md) generalize polymorphic methods to dependent function values and types. _Current status_: There is a proposal, and a prototype implementation, but the implementation has not been finalized or merged yet.
111111
- [Kind Polymorphism](other-new-features/kind-polymorphism.md) allows the definition of operators working equally on types and type constructors.
112112
- [@targetName Annotations](other-new-features/targetName.md) make it easier to interoperate with code written in other languages and give more flexibility for avoiding name clashes.
113113

0 commit comments

Comments
 (0)