考虑下面的代码的理解推理... type TypeOne() =
member val Name = "" with get, set
type TypeTwo() =
member val Name = "" with get, set
member val Property = 0 with get, set
[<RequireQualifiedAcces
首先,对于糟糕的标题表示歉意 - 我不明白足够的F#来更好地描述问题。 考虑一个简单的DU: type Money =
| USD of decimal
| GBP of decimal
| EUR of decimal
static member (+) (first: Money, second: Money) =
match first,
我要实现原语 type point = double * double
type shape =
| Point of point
| Line of point * point
| Vector of point
| Circle of point * double
with
member this.ToString = functi
我想限制工会类型A和B类型的参数,其中B是一些通用类型,它将是子类型。我想提出的对象在这个方法: def accept[A](a:A)(implicit ev:FooOrBaish[A]){ /* do something */}
这是,我怎么指定implicits: case class Foo(i:Int)
trait Baish
case object Bar extends Bai
众所周知,类继承是“开放”类型 - 分区和歧视联盟是“封闭”类型 - 分区。但是,虽然添加新的子类很容易,但添加新的虚拟函数需要修改所有现有的类。同时,受歧视的工会可以轻松添加新的功能。 | inheritance | discriminated union
new type | easy | hard
new function | hard | easy
OOP已经有足够的,
假设我有以下DU: type Something =
| A of int
| B of string * int
现在我用它的功能是这样的: let UseSomething = function
| A(i) -> DoSomethingWithA i
| B(s, i) -> DoSomethingWithB s i
这样的作品,但我不得不解构DU为了将它传递给DoSomet