typelevel / scala

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

Cannot infer literal types in a container #151

Closed Atry closed 6 years ago

Atry commented 7 years ago
Welcome to the Ammonite Repl 0.9.3
(Scala 2.12.2-bin-typelevel-4 Java 1.8.0_102)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ repl.compiler.settings.YliteralTypes.value = true   
@ def someLiteral[SString <: String with Singleton](some: Some[SString]) = "OK" 
defined function someLiteral
@ someLiteral(Some("foo")) 
cmd5.sc:1: inferred type arguments [String] do not conform to method someLiteral's type parameter bounds [SString <: String with Singleton]
val res5 = someLiteral(Some("foo"))
           ^
cmd5.sc:1: type mismatch;
 found   : Some[String]
 required: Some[SString]
val res5 = someLiteral(Some("foo"))
                           ^
Compilation Failed
Atry commented 7 years ago

@milessabin Is there any way to infer it to Some["foo"] except explicit "foo".narrow?

soronpo commented 7 years ago

FYI, It's the same in dotty:

error: type mismatch:
 found   : Some[String]
 required: Some[String & Singleton]
someLiteral(Some("foo"))
soronpo commented 7 years ago

@Atry, if you want to hold on to the narrowed type, you might want to checkout TwoFace, as described here.

milessabin commented 6 years ago

I think this is essentially the same issue as #145 ... closing for now. We can revisit later if need be.