-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Document polymorphic function types #10663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
docs/docs/reference/new-types/polymorphic-function-types.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,93 @@ | ||||||
--- | ||||||
layout: doc-page | ||||||
title: "Polymorphic Function Types" | ||||||
--- | ||||||
|
||||||
A polymorphic function type is a function type which accepts type parameters. | ||||||
For example: | ||||||
```scala | ||||||
// A polymorphic method: | ||||||
def foo[A](xs: List[A]): List[A] = xs.reverse | ||||||
|
||||||
// A polymorphic function value: | ||||||
val bar: [A] => List[A] => List[A] | ||||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||
// a polymorphic function type | ||||||
= [A] => (xs: List[A]) => foo[A](xs) | ||||||
``` | ||||||
Scala already has _polymorphic methods_, i.e. methods which accepts type parameters. | ||||||
Method `foo` above is an example, accepting a type parameter `A`. | ||||||
So far, it | ||||||
was not possible to turn such methods into polymorphic function values like `bar` above, | ||||||
which can be passed as parameters to other functions, or returned as results. | ||||||
|
||||||
In Dotty this is now possible. The type of the `bar` value above is | ||||||
|
||||||
```scala | ||||||
[A] => List[A] => List[A] | ||||||
``` | ||||||
|
||||||
This type describes function values which take a type `A` as a parameter, | ||||||
then take a list of type `List[A]`, and return a list of the same type `List[A]`. | ||||||
|
||||||
[More details](https://github.com/lampepfl/dotty/pull/4672) | ||||||
|
||||||
|
||||||
### Example Usage | ||||||
|
||||||
Polymorphic function type are particularly useful | ||||||
when callers of a method are required to provide a | ||||||
function which has to be polymorphic, | ||||||
meaning that it should accept arbitrary types as part of its inputs. | ||||||
|
||||||
For instance, consider the situation where we have | ||||||
a data type to represent the expressions of a simple language | ||||||
(consisting only of variables and function application) | ||||||
in a strongly-typed way: | ||||||
|
||||||
```scala | ||||||
enum Expr[A]: | ||||||
case Var(name: String) | ||||||
case Apply[A, B](fun: Expr[B => A], arg: Expr[B]) extends Expr[A] | ||||||
``` | ||||||
|
||||||
We would like to provide a way for users to map a function | ||||||
over all immediate subexpressions of a given `Expr`. | ||||||
This requires the given function to be polymorphic, | ||||||
since each subexpression may have a different type. | ||||||
Here is how to implement this using polymorphic function types: | ||||||
|
||||||
```scala | ||||||
def mapSubexpressions[A](e: Expr[A])(f: [B] => Expr[B] => Expr[B]): Expr[A] = | ||||||
e match | ||||||
case Apply(fun, arg) => Apply(f(fun), f(arg)) | ||||||
case Var(n) => Var(n) | ||||||
``` | ||||||
|
||||||
And here is how to use this function to _wrap_ each subexpression | ||||||
in a given expression with a call to some `wrap` function, | ||||||
defined as a variable: | ||||||
|
||||||
```scala | ||||||
val e0 = Apply(Var("f"), Var("a")) | ||||||
val e1 = mapSubexpressions(e0)( | ||||||
[B] => (se: Expr[B]) => Apply(Var[B => B]("wrap"), se)) | ||||||
println(e1) // Apply(Apply(Var(wrap),Var(f)),Apply(Var(wrap),Var(a))) | ||||||
``` | ||||||
|
||||||
|
||||||
|
||||||
### Relationship With Type Lambdas | ||||||
|
||||||
Polymorphic function types are not to be confused with | ||||||
[_type lambdas_](new-types/type-lambdas.md). | ||||||
While the former describes the _type_ of a polymorphic _value_, | ||||||
the latter is an actual function value _at the type level_. | ||||||
|
||||||
A good way of understanding the difference is to notice that | ||||||
**_type lambdas are applied in types, | ||||||
whereas polymorphic functions are applied in terms_**: | ||||||
One would call the function `bar` above | ||||||
by passing it a type argument `bar[Int]` _within a method body_. | ||||||
On the other hand, given a type lambda such as `type F = [A] =>> List[A]`, | ||||||
one would call `F` _withing a type expression_, as in `type Bar = F[Int]`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.