Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Type parameter clause inference for lambdas (and method references via eta-expansion) #18169

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

Draft
smarter wants to merge 2 commits into scala:main
base: main
Choose a base branch
Loading
from dotty-staging:poly-abstract-2

Conversation

Copy link
Member

@smarter smarter commented Jul 7, 2023
edited
Loading

This PR implements type parameter clause inference which lets us write a regular lambda where a polymorphic function type is expected:

val a: [T] => T => String = x => x.toString
// Inferred: [T] => (x: T) => x.toString

Additionally, we also allow regular eta-expansion (which adapts a method reference foo into an untyped lambda x => foo(x)) to proceed when the expected type is a polymorphic function type, the untyped lambda can then be adapted into a polymorphic lambda by type parameter clause inference:

val b: [T] => T => Option[T] = Option.apply
// Inferred: [T] => (x: T) => Option.apply(x)

This is similar to the proposed SIP-49 but instead of inferring the type parameter clause from the method reference we end up inferring it from the expected type, which means we allow the method reference and expected type to have different numbers of type parameters:

def pair[S, T](x: S, y: T): (S, T) = (x, y)
val c: [U] => (Int, U) => (Int, U) = pair
// Inferred: [U] => (x: Int, y: U) => pair[Int, U](x, y)

TODO:

  • Propose an update to SIP-49 which matches this implementation.
  • Move the code under a scala.language.experimental import.

julienrf and benikm91 reacted with rocket emoji benikm91 reacted with eyes emoji
smarter added 2 commits August 7, 2023 15:28
When a lambda is written without a type parameter clause but the expected type
is a polymorphic function type, try to adapt the lambda into a polymorphic
lambda by inferring an appropriate type parameter clause.
This change broke one example in spire which relied on implicit conversions.
The fix has been accepted upstream: typelevel/spire#1247 
In that case, let eta-expansion produce an untyped monomorphic lambda as usual.
Thanks to the previous commit, a type parameter clause for this lambda will be
inferred if possible.
Copy link
Member Author

smarter commented Aug 7, 2023

One presentation compiler test broke:

Error: Test dotty.tools.pc.tests.completion.CompletionKeywordSuite.type-template failed: java.lang.AssertionError: 
Error: Code snippet:
Error: 
Error: package foo
Error: 
Error: class Foo {
Error: typ@@
Error: }
Error: 
Error: 
Error: (+++ Expected, --- Obtained, NO CHANGES)
Error: 
Error: - typeClauseInference - scala.runtime.stdLibPatches.language.experimental
Error: type
Error: 
Error: , took 0.017 sec

@rochala How does auto-completion decide which packages to look into? We should blacklist scala.runtime because it's only supposed to be called from compiler-generated code.

Copy link
Contributor

rochala commented Aug 8, 2023

Hey, so basically this completion is provided from the indexed sources. It indexes whole classpath. If you want to add it to blacklist, it should happen in the symbol search visitor class https://github.com/rochala/dotty/blob/36fdbc5f6f695371fbc64cbcaac80c91f3fa5bc4/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala#L559-L595
You may also try to ignore it during indexing. It is up to you.

If we ignore filter globally, it won't be included even in completions from scope, which is undesired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /