scalashapelesssingleton-typescala-2.12scala-2.13

Scala cross compiling literal types


Is there a way to cross compile literal types to Scala 2.12?

Let's say

def foo[S <: String](implicit V: ValueOf[S]): String = V.value

println(foo["bar"])

Shapeless can encode literal types with shapeless.Witness, so it should be possible to shim ValueOf, or provide another type class with Scala version specific implementations. But I'm a bit lost about what do to do with the expression foo["bar"].


Solution

  • foo["bar"] isn't parsable in 2.12.

    You should use foo[Witness.`"bar"`.T]

    def foo[S <: String](implicit V: Witness.Aux[S]): String = V.value
    
    println(foo[Witness.`"bar"`.T]) //bar
    

    Get type of a "singleton type"