【问题标题】:Scala cross compiling literal typesScala 交叉编译文字类型
【发布时间】:2020-06-29 00:46:22
【问题描述】:

有没有办法将文字类型交叉编译到 Scala 2.12?

我们说

def foo[S <: String](implicit V: ValueOf[S]): String = V.value

println(foo["bar"])

Shapeless 可以使用shapeless.Witness 编码文字类型,因此应该可以填充ValueOf,或者提供另一个具有Scala 版本特定实现的类型类。但我对如何处理表达式 foo["bar"] 有点迷茫。

【问题讨论】:

    标签: scala shapeless singleton-type scala-2.12 scala-2.13


    【解决方案1】:

    foo["bar"] 在 2.12 中不可解析。

    你应该使用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"

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2012-02-17
    • 2011-03-13
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多