Home
Insert
Update snippet '.NET Interop'
Title
Description
Facilities for interop with languages supporting null.
Source code
// [snippet:Interop] [<AutoOpen>] module Interop = let inline (===) a b = obj.ReferenceEquals(a, b) let inline (<=>) a b = not (a === b) let inline isNull value = value === null let inline nil<'T> = Unchecked.defaultof<'T> let inline safeUnbox value = if isNull value then nil else unbox value let (|Null|_|) value = if isNull value then Some() else None // [/snippet] // [snippet:Example] type Foo() = class end type Test() = member this.AcceptFoo(foo:Foo) = //passed from C# if isNull foo then nullArg "foo" else () member this.AcceptFoo2(foo:Foo) = //passed from C# match foo with | Null -> nullArg "foo" | _ -> () member this.AcceptBoxedFoo(boxedFoo:obj) = let foo : Foo = safeUnbox boxedFoo () member this.ReturnFoo() : Foo = //returning to C# if (true) then new Foo() else nil // [/snippet]
Tags
interop
null handling
interop
null handling
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update