4 people like it.

.NET Interop

Facilities for interop with languages supporting null.

Interop

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
[<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

Example

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
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
Multiple items
type AutoOpenAttribute =
  inherit Attribute
  new : unit -> AutoOpenAttribute
  new : path:string -> AutoOpenAttribute
  member Path : string

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
val a : 'a
val b : 'b
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
System.Object.ReferenceEquals(objA: obj, objB: obj) : bool
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val isNull : value:'a -> bool

Full name: Script.Interop.isNull
val value : 'a
val nil<'T> : 'T

Full name: Script.Interop.nil
module Unchecked

from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T

Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
val safeUnbox : value:'a -> 'b

Full name: Script.Interop.safeUnbox
val unbox : value:obj -> 'T

Full name: Microsoft.FSharp.Core.Operators.unbox
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
type Foo =
  new : unit -> Foo

Full name: Script.Foo

--------------------
new : unit -> Foo
Multiple items
type Test =
  new : unit -> Test
  member AcceptBoxedFoo : boxedFoo:obj -> unit
  member AcceptFoo : foo:Foo -> unit
  member AcceptFoo2 : foo:Foo -> unit
  member ReturnFoo : unit -> Foo

Full name: Script.Test

--------------------
new : unit -> Test
val this : Test
member Test.AcceptFoo : foo:Foo -> unit

Full name: Script.Test.AcceptFoo
val foo : Foo
val nullArg : argumentName:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.nullArg
member Test.AcceptFoo2 : foo:Foo -> unit

Full name: Script.Test.AcceptFoo2
active recognizer Null: 'a -> unit option

Full name: Script.Interop.( |Null|_| )
member Test.AcceptBoxedFoo : boxedFoo:obj -> unit

Full name: Script.Test.AcceptBoxedFoo
val boxedFoo : obj
member Test.ReturnFoo : unit -> Foo

Full name: Script.Test.ReturnFoo

More information

Link:http://fssnip.net/4G
Posted:12 years ago
Author:Daniel Robinson
Tags: interop , null handling