Skip to content

Value parameter inference for polymorphic lambdas #18041

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 1 commit into from
Jun 26, 2023

Conversation

smarter
Copy link
Member

@smarter smarter commented Jun 23, 2023

Release Notes

Value parameter inference for polymorphic lambdas

It is now possible to write a polymorphic lambda without writing down the types of its value parameters as long as they can be inferred from the context, for example:

val f: [T] => T => String = [T] => (x: T) => x.toString

can now be written more concisely as:

val f: [T] => T => String = [T] => x => x.toString

Detailed Description (not part of the release notes)

Just like

val f: Int => Int = x => x

is inferred to

val f: Int => Int = (x: Int) => x

we now accept

val g: [T] => T => T = [S] => x => x

which is inferred to

val g: [T] => T => T = [S] => (x: S) => x

This requires a substitution step which is tricky to do since we're operating with untyped trees at this point. We implement this by generalizing the existing DependentTypeTree mechanism (already used for computing dependent result types of lambdas) to also be usable in any other position inside the lambda.

We rename the tree to InLambdaTypeTree at the same time for clarity.

Note that this mechanism could also probably be used to allow closures with internal value parameter dependencies, but we don't attempt to support this here.

Just like

    val f: Int => Int = x => x

is inferred to

    val f: Int => Int = (x: Int) => x

we now accept

    val g: [T] => T => T = [S] => x => x

which is inferred to

    val g: [T] => T => T = [S] => (x: S) => x

This requires a substitution step which is tricky to do since we're operating
with untyped trees at this point. We implement this by generalizing the existing
`DependentTypeTree` mechanism (already used for computing dependent result types
of lambdas) to also be usable in any other position inside the lambda.

We rename the tree to `InLambdaTypeTree` at the same time for clarity.

Note that this mechanism could also probably be used to allow closures with
internal value parameter dependencies, but we don't attempt to support this
here.
@smarter smarter added the release-notes Should be mentioned in the release notes label Jun 23, 2023
@smarter smarter changed the title Value parameter inference for polymorphic lambdas Term parameter inference for polymorphic lambdas Jun 23, 2023
@smarter smarter changed the title Term parameter inference for polymorphic lambdas Value parameter inference for polymorphic lambdas Jun 23, 2023
@smarter smarter requested a review from odersky June 23, 2023 12:27
@odersky odersky merged commit e0afef4 into scala:main Jun 26, 2023
@odersky odersky deleted the poly-eta3 branch June 26, 2023 12:35
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Should be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants