Skip to content

Commit

Permalink
Adds tryGetValue to Option/ValueOption
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Jan 6, 2022
1 parent e8d4410 commit 3943717
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/FsToolkit.ErrorHandling/Option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ module Option =
match parsed with
| true -> Some output
| _ -> None

let inline tryGetValue key dictionary =
let mutable output = Unchecked.defaultof< ^Value >

let parsed =
(^Dictionary: (member TryGetValue : string * byref< ^Value > -> bool) (dictionary, key, &output))

match parsed with
| true -> Some output
| false -> None
#endif

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions src/FsToolkit.ErrorHandling/ValueOption.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ module ValueOption =
| true -> ValueSome output
| _ -> ValueNone

let inline tryGetValue key dictionary =
let mutable output = Unchecked.defaultof< ^Value >

let parsed =
(^Dictionary: (member TryGetValue : string * byref< ^Value > -> bool) (dictionary, key, &output))

match parsed with
| true -> ValueSome output
| false -> ValueNone

// let foo () =
// let mydict = dict [ "hello", "world" ]
// match mydict |> tryGetValue "hello" with
// | ValueSome x -> printfn "value: %A" x
// | ValueNone -> printfn "no value"

// let foo2 () =
// let mydict = dict [ "hello", "world" ]
// match mydict.TryGetValue("hello") with
// | (true,x) -> printfn "value: %A" x
// | _ -> printfn "no value"


/// <summary>
/// Takes two voptions and returns a tuple of the pair or none if either are none
/// </summary>
Expand Down

0 comments on commit 3943717

Please sign in to comment.