Skip to content

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

@yurique

Description

@yurique

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions