typelevel / scala

Typelevel Scala, a fork of Scala
http://typelevel.org/scala/
372 stars 21 forks source link

Weird literal types class fields narrowness #165

Closed soronpo closed 6 years ago

soronpo commented 7 years ago

See code comments for the issues:

  object Test {
    val one = 1
    final val final_one = 1

    object OK {
      def foo[T <: Int](t : T) : T = t
      val a : 1 = foo(1)
      val b : Int = foo(one)
      val c : 1 = foo(final_one)
    }

    object Fail {
      case class Foo[T <: Int](t : T)
      val a : 1 = Foo(1).t                  //Error: Type mismatch
      val b : Int = Foo(one).t
      val c : 1 = Foo(final_one).t      //Error: Type mismatch
    }

    object OK2 {
      case class Foo[T <: Int with Singleton](t : T)
      val a : 1 = Foo(1).t
      val b : Int = Foo(one).t           
      val c : 1 = Foo(final_one).t
    }
  }
milessabin commented 6 years ago

Those failures are expected ... in the absence of the Singleton bound I would expect the inferred type to be widened.

soronpo commented 6 years ago

What is the reason the case class behaves differently than a definition?