Skip to content

Instantly share code, notes, and snippets.

@jbtule
Last active May 13, 2022 16:38
Show Gist options
  • Select an option

  • Save jbtule/8477768 to your computer and use it in GitHub Desktop.

Select an option

Save jbtule/8477768 to your computer and use it in GitHub Desktop.

Revisions

  1. jbtule revised this gist Jan 29, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions NullCoalesce.fs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    //inspired by http://stackoverflow.com/a/2812306/637783

    type NullCoalesce =
    static member Coalesce(a: 'a option, b: unit -> 'a) = match a with Some a -> a | _ -> b()
    static member Coalesce(a: 'a Nullable, b: unit -> 'a) = if a.HasValue then a.Value else b()
    static member Coalesce(a: 'a when 'a:null, b: unit -> 'a) = match a with null -> b() | _ -> a
    static member Coalesce(a: 'a option, b: 'a Lazy) = match a with Some a -> a | _ -> b.Value
    static member Coalesce(a: 'a Nullable, b: 'a Lazy) = if a.HasValue then a.Value else b.Value
    static member Coalesce(a: 'a when 'a:null, b: 'a Lazy) = match a with null -> b.Value | _ -> a

    let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
    ((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))
  2. jbtule revised this gist Jan 18, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions NullCoalesce.fs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    //inspired by http://stackoverflow.com/a/2812306/637783

    type NullCoalesce =
    static member Coalesce(a: 'a option, b: unit -> 'a) = if a.IsSome then a.Value else b()
    static member Coalesce(a: 'a option, b: unit -> 'a) = match a with Some a -> a | _ -> b()
    static member Coalesce(a: 'a Nullable, b: unit -> 'a) = if a.HasValue then a.Value else b()
    static member Coalesce(a: 'a when 'a:null, b: unit -> 'a) = if a <> null then a else b()
    static member Coalesce(a: 'a when 'a:null, b: unit -> 'a) = match a with null -> b() | _ -> a

    let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
    ((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))
  3. jbtule revised this gist Jan 17, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions NullCoalesce.fs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    //inspired by http://stackoverflow.com/a/2812306/637783

    type NullCoalesce =
    static member Coalesce(a: 'a option, b: unit -> 'a) = if a.IsSome then a.Value else b()
    static member Coalesce(a: 'a Nullable, b: unit -> 'a) = if a.HasValue then a.Value else b()
  4. jbtule revised this gist Jan 17, 2014. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions NullCoalesce.fs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    //inspired by http://stackoverflow.com/a/2812306/637783

    type NullCoalesce =
    static member Coalesce(a: 'a option, b: 'a) = if a.IsSome then a.Value else b
    static member Coalesce(a: 'a Nullable, b: 'a) = if a.HasValue then a.Value else b
    static member Coalesce(a: 'a when 'a:null, b: 'a) = if a <> null then a else b
    static member Coalesce(a: 'a option, b: unit -> 'a) = if a.IsSome then a.Value else b()
    static member Coalesce(a: 'a Nullable, b: unit -> 'a) = if a.HasValue then a.Value else b()
    static member Coalesce(a: 'a when 'a:null, b: unit -> 'a) = if a <> null then a else b()

    let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
    ((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))
  5. jbtule created this gist Jan 17, 2014.
    11 changes: 11 additions & 0 deletions NullCoalesce.fs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    //inspired by http://stackoverflow.com/a/2812306/637783

    type NullCoalesce =
    static member Coalesce(a: 'a option, b: 'a) = if a.IsSome then a.Value else b
    static member Coalesce(a: 'a Nullable, b: 'a) = if a.HasValue then a.Value else b
    static member Coalesce(a: 'a when 'a:null, b: 'a) = if a <> null then a else b

    let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
    ((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))

    let inline (|??) a b = nullCoalesceHelper<NullCoalesce, _, _, _> a b