typelevel / typelevel/cats

`Const` type inference glitch

Open
#1,608 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Binary Breaking scope: small
Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimized Scala example involving Const, trav, and scalac, then read the three linked comments in the discussion. Compare the inference behavior with and without phantom-type covariance. Done requires an agreed resolution for whether the inconvenience or covariance ramifications should be accepted; no implementation file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.