这个函数有签名:(UnionCaseInfo -> bool) -> 'T option
为什么在这里需要类型注释?
let private findCase<'T> f =
match FSharpType.GetUnionCases typeof<'T> |> Array.filter f with
|[|case|] -> Some (FSharpValue.MakeUnion(case,[||]) :?> 'T)
|_ -> None
此功能,它调用上述功能,具有签名:int -> obj
let CreateFromId<'T> id =
match findCase (fun case -> case.Tag = id) with
| Some c -> c
| None -> failwith (sprintf "Lookup for union case by \"%d\" failed." id)
在用于CreateFromId
图案,智能感知显示c
被推断即使它显示findCase
的正确签名,也是obj
类型。为什么这种类型似乎在模式中“失去”了?
这很有道理。我想我需要有类型参数,因为'FSharpType.GetUnionCases'需要它。 –