Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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';F#中歧视性结合的s对应物?_Scala_F# - Fatal编程技术网

什么是Scala';F#中歧视性结合的s对应物?

什么是Scala';F#中歧视性结合的s对应物?,scala,f#,Scala,F#,如何将in F#转换为Scala: type Expr = | Val of String | Integer of Int32 | Lower of Expr * Expr | Greater of Expr * Expr | And of Expr * Expr | Or of Expr * Expr 有一篇类似的帖子在谈论,但这似乎不是我想要的。这是通过scala中的继承完成的(可能很不幸,因为它更详细) 你可

如何将in F#转换为Scala:

type Expr =
    | Val     of String
    | Integer of Int32
    | Lower   of Expr * Expr
    | Greater of Expr * Expr
    | And     of Expr * Expr
    | Or      of Expr * Expr

有一篇类似的帖子在谈论,但这似乎不是我想要的。

这是通过scala中的继承完成的(可能很不幸,因为它更详细)

你可以进一步打字

sealed trait Expr[A]
case class Val(s: String) extends Expr[String]
case class Integer(i: Int) extends Expr[Int]
case class Lower[X](left: Expr[X], right: Expr[X])(implicit val ordering: Ordering[X]) extends Expr[Boolean]
模式匹配

def valueOf[A](expr: Expr[A]) : A = expr match {
   case Val(s) => s
   case Integer(i) => i
   case l @ Lower(a,b) => l.ordering.lt(valueOf(a), valueOf(b))
   ...
}
valueOf作为Expr中的一种方法可能会更好

sealed trait Expr[A] {def value: A}
case class Val(value: String) extends Expr[String]
case class Integer(value: Int) extends Expr[Int]
case class Lower[X: Ordering](left: Expr[A], right: Expr[A]) extends Expr[Bool] {
   def value = implicitly[Ordering[X]].lt(left.value, right.value)
}
...
我完全同意,但如果您需要更高级别的抽象,在Scala中实现选项类型会提供一个很好的直觉:

密封特征选项[+E]
case类Some[+E](元素:E)扩展选项[E]
案例对象无扩展选项[无]

来源:

非常快,回答了我的问题。非常感谢。这是否提供了详尽的匹配(即,如果存在未处理的情况,则会出现编译器错误)
sealed trait Expr[A] {def value: A}
case class Val(value: String) extends Expr[String]
case class Integer(value: Int) extends Expr[Int]
case class Lower[X: Ordering](left: Expr[A], right: Expr[A]) extends Expr[Bool] {
   def value = implicitly[Ordering[X]].lt(left.value, right.value)
}
...