首先,對於糟糕的標題表示歉意 - 我不明白足夠的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
我已經定義了以下歧視工會的歧視工會: type Expr =
| Con of Num
| Var of Name
| Add of Expr * Expr
| Sub of Expr * Expr
| Mult of Expr * Expr
| Div of Expr * Expr
| Pow of Expr * Expr
衆所周知,類繼承是「開放」類型 - 分區和歧視聯盟是「封閉」類型 - 分區。但是,雖然添加新的子類很容易,但添加新的虛擬函數需要修改所有現有的類。同時,受歧視的工會可以輕鬆添加新的功能。 | 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
考慮下面的代碼的理解推理... type TypeOne() =
member val Name = "" with get, set
type TypeTwo() =
member val Name = "" with get, set
member val Property = 0 with get, set
[<RequireQualifiedAcces