Skip to content

Const type inference glitch #1608

Open
Open
@kailuowang

Description

@kailuowang

Const's phantom type can cause some type inference difficulty in scalac. Here is a minimized example:

   def trav[F[_], A, B](f: A => F[B]): F[B] = ???
    case class Const[A, B](a: A)
    val c: Const[Int, Int] = trav(Const(_:Int))   //compiles
    val a: Int  = trav(Const(_:Int)).a    //doesn’t compile

[error] found : Int => Const[Int,Nothing]
[error] required: Int => Const[Int,B]
[error] val a: Int = trav(Const(_:Int)).a

Making the phantom type covariant will helped the type inference but it's not a very obviously sound solution. See the discussion here by @ceedubs , here by @djspiewak and here by @johnynek.

Shall we live with the type inference inconvenience (not encountered very often though) or the possible ramifications of unnecessarily making the phantom type covariant?

It seems to me that neither incurs significant cost to pay but the former solution is easier to understand.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions