F#では、値が1回だけ割り当てられた識別された共用体を使用できますか?単一値の弁別された組合?
type DogAttributes = { Age: int; Color: string; }
type Dog =
| Rex of DogAttributes ({ Age = 5; Color = "Brown"; }
| Fido of DogAttributes ({ Age = 3; Color = "White"; }
Rexの値は常に割り当てられたDogAttributesを持ち、変更できませんでした。その時点で、あなただけでOOスタイルの多型を利用したほうが良いだろうように思える、
type DogAttribute = { Age : int; Color : string }
type Dog =
| Rex of DogAttribute
| Fido of DogAttribute
[<AbstractClass>]
type DogBase(dog, age, color) =
member x.Age = age
member x.Color = color
member x.Dog = dog { Age = x.Age; Color = x.Color}
type Rex() =
inherit DogBase(Dog.Rex, 5, "Brown")
type Fido() =
inherit DogBase(Dog.Fido, 3, "White")
をしかし:
いいえ、それはできません。私は誰がどのようにそれを使いたいかもわかりません。 – TeaDrivenDev
コンパイル時に、私が使いたい特定のDogの値をすべて知っていれば役に立ちます。明らかに、私はこのデータを他の場所に保存して見ることができます。これは私が今やっていることです。 –
'Rex'と' Fido'は* types *のような気がしません、彼らは* values *のように感じます。あなたの問題のドメインをモデル化するより良い方法は 'let rex = DogAttributes {Age = 5;色= "ブラウン"} '。 – rmunn