gpt4 book ai didi

Scala:如何强制将语句转换为文字?

转载 作者:行者123 更新时间:2023-12-03 18:34:19 26 4
gpt4 key购买 nike

我正在试验它的一个库中提供的 scala 的精炼类型特性:

https://github.com/fthomas/refined

下面的代码代表一个简单的案例:

  import eu.timepit.refined.auto._
import shapeless.{Witness => W}

type Vec5 = List[Int] Refined Size[Equal[W.`5`.T]]

val v1: Vec5 = List(1, 2, 3, 4, 5)

val v2: Vec5 = List(1 to 5: _*)

尝试编译它时,我收到以下错误:

[Error] /home/peng/git/scalaspike/common/src/test/scala/com/tribbloids/spike/refined_spike/Example.scala:32: compile-time refinement only works with literals
[Error] /home/peng/git/scalaspike/common/src/test/scala/com/tribbloids/spike/refined_spike/Example.scala:34: compile-time refinement only works with literals
[Error] /home/peng/git/scalaspike/common/src/test/scala/com/tribbloids/spike/singleton_ops_spike/Example.scala:32: Cannot prove requirement Require[...]
three errors found

应该注意的是,v1 和 v2 都可以在编译时轻松评估和内联,但是 Scala 编译器似乎拒绝这样做,并且对于 List 类型似乎没有办法建议这一点。

那么这个功能怎么会有用呢?

最佳答案

问题是 eu.timepit.refined 宏适用于文字、 BigDecimalBigInt
https://github.com/fthomas/refined/blob/master/modules/core/shared/src/main/scala/eu/timepit/refined/macros/RefineMacro.scala#L14-L23

def impl[F[_, _], T: c.WeakTypeTag, P: c.WeakTypeTag](t: c.Expr[T])(
rt: c.Expr[RefType[F]],
v: c.Expr[Validate[T, P]]
): c.Expr[F[T, P]] = {
val tValue: T = t.tree match {
case Literal(Constant(value)) => value.asInstanceOf[T]
case BigDecimalMatcher(value) => value.asInstanceOf[T]
case BigIntMatcher(value) => value.asInstanceOf[T]
case _ => abort(Resources.refineNonCompileTimeConstant)
}
List(1, 2, 3, 4, 5) 不是文字。

对于像 List(1, 2, 3, 4, 5) 这样的非文字值,有 refineV 在运行时工作
val v1 = List(1, 2, 3, 4, 5)
val v2 = List(1, 2, 3, 4, 5, 6)
refineV[Size[Equal[5]]](v1)
// Right(List(1, 2, 3, 4, 5))
refineV[Size[Equal[5]]](v2)
// Left(Predicate taking size(List(1, 2, 3, 4, 5, 6)) = 6 failed: Predicate failed: (6 == 5).)

幸运的是你可以在编译时运行 refineV
object myAuto {
implicit def compileTimeRefineV[T, P](t: T): T Refined P =
macro compileTimeRefineVImpl[T, P]

def compileTimeRefineVImpl[T: c.WeakTypeTag,
P: c.WeakTypeTag](c: blackbox.Context)(t: c.Tree): c.Tree = {
import c.universe._
val pTyp = weakTypeOf[P]
val tTyp = weakTypeOf[T]
c.eval(c.Expr[Either[String, T Refined P]](c.untypecheck(
q"_root_.eu.timepit.refined.`package`.refineV[$pTyp].apply[$tTyp]($t)"
))).fold(
c.abort(c.enclosingPosition, _),
_ => q"$t.asInstanceOf[_root_.eu.timepit.refined.api.Refined[$tTyp, $pTyp]]"
)
}
}

import myAuto._ // don't import eu.timepit.refined.auto._
type Vec5 = List[Int] Refined Size[Equal[5]]
val v1: Vec5 = List(1, 2, 3, 4, 5) // compiles
// val v2: Vec5 = List(1, 2, 3, 4, 5, 6)
// Error: Predicate taking size(List(1, 2, 3, 4, 5, 6)) = 6 failed: Predicate failed: (6 == 5).

如果您只需要静态大小的集合,您可以使用 shapeless.Sized
https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0#collections-with-statically-known-sizes

关于Scala:如何强制将语句转换为文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61979101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com