就我个人而言,我从来都不喜欢
bool TryXXX(stringToParseOrKeyToLookup, out parsedInputOrLookupValue_DefaultIfParseFailsOrLookupNotFound)
Some
/
None
图案会很完美(比如
Seq.tryFind
type MyClass () =
let items = System.Collections.Generic.Dictionary<string,int>()
do
items.Add ("one",1)
items.Add ("two",2)
items.Add ("three",3)
member this.TryGetValue (key) =
match items.TryGetValue(key) with
| (true, v) -> Some(v)
| _ -> None
let c = MyClass()
let printKeyValue key =
match c.TryGetValue(key) with
| Some(value) -> printfn "key=%s, value=%i" key value
| None -> printfn "key=%s, value=None" key
//> printKeyValue "three";;
//key=three, value=3
//val it : unit = ()
//> printKeyValue "four";;
//key=four, value=None
//val it : unit = ()