How to use literal type in Scala 2.13

Yan*_*san 1 scala scala-2.13

I'm trying Literal Types from Scala 2.13 and I encounter the following error :

scala> def double[A <: Singleton] = valueOf[A]
                                           ^
       error: No singleton value available for A.
Run Code Online (Sandbox Code Playgroud)

Could you explain why ?

cch*_*tep 6

I don't think that's working like that, but rather with the related typeclass ValueOf:

object Foo

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

scala> foo[Foo.type]

res2: Foo.type = Foo$@1c105c3a
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。我现在可以做一些很酷的事情:scala&gt; def add [A &lt;:Int:ValueOf,B &lt;:Int:ValueOf] = valueOf [A] + valueOf [B] scala&gt; add [1,2] res79:Int = 3 (3认同)
  • @PavelFilatov,您可能想要检查单例操作库以了解更多信息。 (2认同)