Skip to content

Instantly share code, notes, and snippets.

@gusty
Created June 15, 2018 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusty/70b1dae708c3ee41ac6f9ceb257264e6 to your computer and use it in GitHub Desktop.
Save gusty/70b1dae708c3ee41ac6f9ceb257264e6 to your computer and use it in GitHub Desktop.
Efficiently constrained isNull
type A = class end
type B = class inherit A end
type C = class inherit B end
type D = class inherit C end
type IsNull =
inherit D
static member inline ($) (_:A , x :'a when 'a : not struct) = System.Object.ReferenceEquals(x, null)
static member inline ($) (_:B , x :'a when 'a : null ) = match x with null -> true | _ -> false
static member inline ($) (_:C , _:D) = false
static member inline ($) (_:D , _:C) = false
let inline isNull x = Unchecked.defaultof<IsNull> $ x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment