Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

smarter
Copy link
Member

@smarter smarter commented Jul 7, 2023

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.

@smarter smarter force-pushed the poly-abstract-2 branch 2 times, most recently from 298eb0d to fc0476f Compare July 18, 2023 13:44
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.
@smarter
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.

@rochala
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants