讓這個類型=如何將函數應用於變體?
type intC = int;;
type boolC = bool;
type stringC = string;;
type component = A of intC | B of boolC | C of stringC;;
如果我想在一個類型的組件A的應用功能,還需要系統地解構成分?
用於爲例做我必須做的:
let add comp =
match comp with
| A i -> Some (i + 2) (*only A interests me, I return i + 2*)
| _ -> None (*otherwise I return nothing*)
,然後在A組份的任何功能?有沒有任何意思來避免你的冗餘?
我明白了;這是處理這個問題的標準方法嗎?是否有可能使模塊更具通用性? – codablank1 2012-04-26 17:50:41
這取決於數據表示的內容。我從來沒有需要做這樣的事情,並會破壞所有的元素,但我也不會經常解構,並會有適當的'B'和'C'的情況。 – nlucaroni 2012-04-26 17:54:03