Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 为什么';Nat是否有方法在运行时访问其值?_Scala_Shapeless - Fatal编程技术网

Scala 为什么';Nat是否有方法在运行时访问其值?

Scala 为什么';Nat是否有方法在运行时访问其值?,scala,shapeless,Scala,Shapeless,下面是一个示例程序,该程序要求shapeless.Nat能够在运行时生成其值,即使其值在滑稽时未知: case class Command[N <: Nat](name: String, args: Sized[List[String], N]) case class InvokeCommand[N <: Nat, A](ref: Command[N], args: Sized[List[A], N]) object Program { val commands: List[

下面是一个示例程序,该程序要求
shapeless.Nat
能够在运行时生成其值,即使其值在滑稽时未知:

case class Command[N <: Nat](name: String, args: Sized[List[String], N])
case class InvokeCommand[N <: Nat, A](ref: Command[N], args: Sized[List[A], N])

object Program {

  val commands: List[Command[Nat]] = List(
    Command("foo", Sized("t", "r", "f")),
    Command("bar", Sized("h", "i")))

  def callCommand[A](name: String, args: List[A]): Option[InvokeCommand] = {
    val command = commands.find(_.name ≟ name)
    args.sized(command.args.size).map(InvokeCommand(command, _))
  }
}

case类命令[N目的是使用
Nat
类型主要是虚的,并通过类型推断和隐式解析影响类型检查和计算。在这种情况下,不需要任何运行时表示,所需的额外存储将被浪费。在需要运行时表示的情况下另一方面,
ToInt
类型类可以提供它,尽管显然比内在的
ToInt
方法更具仪式性


总之,这是“精心设计的”:事情本来可以做得不同,但它们是按预期的那样做的。

使用正确的导入(
import shapeless.syntax.nat
nat
有一个
toInt
方法。请参见@gabrielepetonella Yes,但
toInt
方法需要实际的“值”编译时已知的
Nat
。这是因为它需要一个
ToInt
typeclass的实例,而shapeless只为
Succ
\u 0
提供实例。大概shapeless这样做是为了避免能够对类型为
Nat
的值调用
ToInt
s没有更具体的类型(这意味着它的值在编译时是未知的),我想这是我问题的关键所在。