Skip to content

default value for a function parameter - the compiler complains about the type, but the type is correct #18795

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

Closed
yurique opened this issue Oct 30, 2023 · 2 comments · Fixed by #18798

Comments

@yurique
Copy link

yurique commented Oct 30, 2023

Compiler version

3.3.1

Minimized code

package example

object Main extends App with Test {
  load("")()
}

trait Test {

  def load[T](
    doLoad: T
  )(
    description: T => Option[String] = (_: T) => None // <--- compile error here
  ): Unit = ???

}

(sbt project available here: https://github.com/yurique/scala3-weird)

Output

[error] 12 |    description: T => Option[String] = (_: T) => None
[error]    |                                       ^^^^^^^^^^^^^^
[error]    |                        Found:    T => Option[String]
[error]    |                        Required: <? >: ("" : String)> => Option[String]
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] one error found

Expectation

The code should compile.

Notes

  • this code compiles if the load function is defined in an object (no in a trait)
  • it compiles if I move the definition of the Test trait below the definition of the Main object
  • It also fails to compile with the same error if the function is defined:
    • as a top-level definition
    • inside a package object
    • as a trait in a separate file

edit: smaller example - no custom case classes needed

@yurique yurique added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 30, 2023
@nicolasstucki
Copy link
Contributor

Minimized

def test: Unit = load("")()
def load[T](doLoad: T)(description: T => Int = (x: T) => 3): Unit = ???

@nicolasstucki
Copy link
Contributor

It works if we swap the order of the definitions. Some constraint might be leaking from load("")() into def load[T]....

@nicolasstucki nicolasstucki added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 30, 2023
odersky added a commit to dotty-staging/dotty that referenced this issue Oct 30, 2023
Tweak approximation of type variables when computing the expected types
of default arguments.

Fixes scala#18795
odersky added a commit that referenced this issue Oct 31, 2023
…18798)

Tweak approximation of type variables when computing the expected types
of default arguments.

Fixes #18795
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants