|
| 1 | +namespace FsToolkit.ErrorHandling |
| 2 | + |
| 3 | + |
| 4 | +#if !FABLE_COMPILER |
| 5 | +[<AutoOpen>] |
| 6 | +module ValueOptionCE = |
| 7 | + open System |
| 8 | + |
| 9 | + type ValueOptionBuilder() = |
| 10 | + member inline _.Return(x) = ValueSome x |
| 11 | + |
| 12 | + member inline _.ReturnFrom(m: 'T voption) = m |
| 13 | + |
| 14 | + member inline _.Bind(m: 'a voption, f: 'a -> 'b voption) = ValueOption.bind f m |
| 15 | + |
| 16 | + // Could not get it to work solely with Source. In loop cases it would potentially match the #seq overload and ask for type annotation |
| 17 | + member this.Bind(m: 'a when 'a: null, f: 'a -> 'b voption) = this.Bind(m |> ValueOption.ofObj, f) |
| 18 | + |
| 19 | + member inline this.Zero() = this.Return() |
| 20 | + |
| 21 | + member inline _.Combine(m, f) = ValueOption.bind f m |
| 22 | + member inline this.Combine(m1: _ voption, m2: _ voption) = this.Bind(m1, (fun () -> m2)) |
| 23 | + |
| 24 | + member inline _.Delay(f: unit -> _) = f |
| 25 | + |
| 26 | + member inline _.Run(f) = f () |
| 27 | + |
| 28 | + member inline this.TryWith(m, h) = |
| 29 | + try |
| 30 | + this.Run m |
| 31 | + with |
| 32 | + | e -> h e |
| 33 | + |
| 34 | + member inline this.TryFinally(m, compensation) = |
| 35 | + try |
| 36 | + this.Run m |
| 37 | + finally |
| 38 | + compensation () |
| 39 | + |
| 40 | + member inline this.Using(resource: 'T :> IDisposable, binder) : _ voption = |
| 41 | + this.TryFinally( |
| 42 | + (fun () -> binder resource), |
| 43 | + (fun () -> |
| 44 | + if not <| obj.ReferenceEquals(resource, null) then |
| 45 | + resource.Dispose()) |
| 46 | + ) |
| 47 | + |
| 48 | + member this.While(guard: unit -> bool, generator: unit -> _ voption) : _ voption = |
| 49 | + if not <| guard () then |
| 50 | + this.Zero() |
| 51 | + else |
| 52 | + this.Bind(this.Run generator, (fun () -> this.While(guard, generator))) |
| 53 | + |
| 54 | + member inline this.For(sequence: #seq<'T>, binder: 'T -> _ voption) : _ voption = |
| 55 | + this.Using( |
| 56 | + sequence.GetEnumerator(), |
| 57 | + fun enum -> this.While(enum.MoveNext, this.Delay(fun () -> binder enum.Current)) |
| 58 | + ) |
| 59 | + |
| 60 | + member inline _.BindReturn(x, f) = ValueOption.map f x |
| 61 | + |
| 62 | + member inline _.BindReturn(x, f) = |
| 63 | + x |> ValueOption.ofObj |> ValueOption.map f |
| 64 | + |
| 65 | + member inline _.MergeSources(option1, option2) = ValueOption.zip option1 option2 |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type. |
| 69 | + /// |
| 70 | + /// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder |
| 71 | + /// </summary> |
| 72 | + member inline _.Source(result: _ voption) : _ voption = result |
| 73 | + |
| 74 | + |
| 75 | + // /// <summary> |
| 76 | + // /// Method lets us transform data types into our internal representation. |
| 77 | + // /// </summary> |
| 78 | + member inline _.Source(vopt: _ option) : _ voption = vopt |> ValueOption.ofOption |
| 79 | + |
| 80 | + let voption = ValueOptionBuilder() |
| 81 | + |
| 82 | +[<AutoOpen>] |
| 83 | +// Having members as extensions gives them lower priority in |
| 84 | +// overload resolution and allows skipping more type annotations. |
| 85 | +module ValueOptionExtensionsLower = |
| 86 | + type ValueOptionBuilder with |
| 87 | + member inline _.Source(nullableObj: 'a when 'a: null) = nullableObj |> ValueOption.ofObj |
| 88 | + member inline _.Source(m: string) = m |> ValueOption.ofObj |
| 89 | + |
| 90 | + member inline _.MergeSources(nullableObj1, option2) = |
| 91 | + ValueOption.zip (ValueOption.ofObj nullableObj1) option2 |
| 92 | + |
| 93 | + |
| 94 | + member inline _.MergeSources(option1, nullableObj2) = |
| 95 | + ValueOption.zip (option1) (ValueOption.ofObj nullableObj2) |
| 96 | + |
| 97 | + |
| 98 | + member inline _.MergeSources(nullableObj1, nullableObj2) = |
| 99 | + ValueOption.zip (ValueOption.ofObj nullableObj1) (ValueOption.ofObj nullableObj2) |
| 100 | + |
| 101 | +[<AutoOpen>] |
| 102 | +// Having members as extensions gives them lower priority in |
| 103 | +// overload resolution and allows skipping more type annotations. |
| 104 | +// The later declared, the higher than previous extension methods, this is magic |
| 105 | +module ValueOptionExtensions = |
| 106 | + open System |
| 107 | + |
| 108 | + type ValueOptionBuilder with |
| 109 | + /// <summary> |
| 110 | + /// Needed to allow `for..in` and `for..do` functionality |
| 111 | + /// </summary> |
| 112 | + member inline _.Source(s: #seq<_>) = s |
| 113 | + |
| 114 | + // /// <summary> |
| 115 | + // /// Method lets us transform data types into our internal representation. |
| 116 | + // /// </summary> |
| 117 | + member inline _.Source(nullable: Nullable<'a>) : 'a voption = nullable |> ValueOption.ofNullable |
| 118 | +#endif |
0 commit comments