一個非常方便的功能是,你可以做內聯和保值型的多態性一定程度的(雖然我認爲「鴨打字」更貼切):爲什麼內聯函數的類型推斷強迫參數爲某種類型,而不是將其限制爲具有op_Explicit?在F#
// can be used with any argument that implements op_Explicit: ^a -> float
let inline Divide a b = float a/float b
但是當我把它擴大到被包含在某種類型中,F#推斷第一個參數爲float
,即使我明確要求轉換爲浮動。我錯過了什麼,或者更好,我怎樣才能得到op_Explicit
的行爲?我嘗試添加靜態成員的約束,但似乎並沒有幫助:
type XTest<'T> =
| Value of 'T
| Other of 'T
// a is inferred as float, b as req. op_Explicit
static member inline Divide a b =
match a with
| Value x ->
match b with
| Value y ->
let res = float x/float y
XTest.Value res |> Some
| _ -> failwith "not implemented"
| Other x-> Some (XTest.Other x)
也許重要的是要注意:如果刪除Other
識別聯合,它推斷出第一個參數的類型進行正確的「要求各會員op_Explicit「
謝謝,優秀的答案,一如既往:)。它也隱含地顯示瞭解決方案。我只需要讓類型推斷知道'x'不知道是'float'類型,我通過將最後一行改爲'Some(XTest.Other(float x))'來完成。 – Abel