Skip to content

A and (=> A) do not unify when used as function type parameters #7431

Open
@scabug

Description

@scabug

It doesn't seem possible to use (=> A) => B in contexts requiring A => B:

scala> def log(a: => Any): Unit = println(a)
log: (a: => Any)Unit

scala> def apply[A, B](a: A)(f: A => B) = f(a)
apply: [A, B](a: A)(f: A => B)B

scala> apply("hello")(log)
<console>:10: error: type mismatch;
 found   : (=> Any) => Unit
 required: Int => ?
              apply("hello")(log)
                             ^

If you try to go the other direction, using A => B in contexts requiring (=> A) => B, the result depends on how you're trying to do it:

scala> def log(a: Any) = println(a)
log: (a: Any)Unit

scala> def apply[A, B](a: A)(f: (=> A) => B) = f(a)
apply: [A, B](a: A)(f: (=> A) => B)B

scala> apply("hello")(log)
<function0>

scala> apply("hello")(identity)
java.lang.ClassCastException: $anonfun$apply$1 cannot be cast to java.lang.String

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions