gogstad
gogstad

Reputation: 3739

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

Upvotes: 1

Views: 234

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51693

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"

Upvotes: 2

Related Questions