简体   繁体   中英

How to use literal type in 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.

Could you explain why ?

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

I am not entirely sure of what you're trying, but here an example from the 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

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