简体   繁体   中英

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"] .

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM