如何在 Scala 2.13 中使用文字类型

How to use literal type in Scala 2.13

我正在尝试 Scala 2.13 中的文字类型,但遇到以下错误:

scala> def double[A <: Singleton] = valueOf[A]
                                           ^
       error: No singleton value available for A.

你能解释一下为什么吗?

我不认为它是那样工作的,而是与相关的类型类 ValueOf:

object Foo

def foo[A : ValueOf] = valueOf[A]

scala> foo[Foo.type]

res2: Foo.type = Foo$@1c105c3a

我不完全确定你在尝试什么,但这里有一个来自 doc:

的例子
def foo[T](implicit v: ValueOf[T]): T = v.value

A scala.ValueOf[T] type class and corresponding scala.Predef.valueOf[T] operator has been added yielding the unique value of types with a single inhabitant