typelevel / scala

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

Cannot print wildcard singleton type #156

Closed tindzk closed 6 years ago

tindzk commented 7 years ago

It is possible to instantiate a singleton type parameter with a wildcard, but not print it:

$ curl -s https://raw.githubusercontent.com/typelevel/scala/typelevel-readme/try-typelevel-scala.sh | bash

Loading...
Welcome to the Ammonite Repl 0.8.4
(Scala 2.12.2-bin-typelevel-4 Java 1.8.0_112)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ repl.compiler.settings.YliteralTypes.value = true  

@ case class Wrap[T <: Singleton](value: String with T) 
defined class Wrap
@ def f(): Wrap[_] = Wrap("test") 
defined function f
@ f() 
cmd3.sc:8: type arguments [_$1] do not conform to class Wrap's type parameter bounds [T <: Singleton]
            .print(res3, res3, "res3", _root_.scala.None)                                                                                                                               
             ^                                                                                                                                                                          
cmd3.sc:8: type arguments [_$1] do not conform to class Wrap's type parameter bounds [T <: Singleton]
            .print(res3, res3, "res3", _root_.scala.None)                                                                                                                               
                  ^                                                                                                                                                                     
Compilation Failed
@ f().value 
res3: String with Any = "test"

Edit: Here is a minimised example:

case class Wrap[T <: Singleton](value: T)
def f[T](value: T): Unit = {}
f(null.asInstanceOf[Wrap[_]])

The error occurs whenever Wrap[_] is passed as a function argument.

milessabin commented 7 years ago

The error message here is unhelpful, but I'm not entirely sure I understand why this should compile. Notice that your minimised example fails in exactly the same way with Lightbend Scala, as I think it should.

tindzk commented 7 years ago

Indeed. The following example would fail as well:

case class MyList[T <: String]()
def f[T](value: T): Unit = {} 
f(null.asInstanceOf[MyList[_]])

I agree that the error message should be more comprehensible. The problem can be fixed by taking Any instead of T. However, I do not understand yet why wildcards cannot be used in conjunction with type parameter bounds.

The other issue where the value cannot be printed seems to be a bug in Ammonite.

milessabin commented 6 years ago

I'm closing this because it doesn't appear to be a TLS-specific problem.