3 people like it.

Nullable Refs without using AllowNullLiteral

Nullable Refs without using AllowNullLiteral

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
let (@?) (a:'a) (b:unit -> 'a) : 'a = 
  if System.Object.ReferenceEquals(a :> obj, null) 
    then b()
    else a
    
type Foo() = 
  class 
  end

let x = Unchecked.defaultof<Foo>
let z = x @? (fun()->Foo())
val a : 'a
val b : (unit -> 'a)
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
namespace System
Multiple items
type Object =
  new : unit -> obj
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member GetType : unit -> Type
  member ToString : unit -> string
  static member Equals : objA:obj * objB:obj -> bool
  static member ReferenceEquals : objA:obj * objB:obj -> bool

Full name: System.Object

--------------------
System.Object() : unit
System.Object.ReferenceEquals(objA: obj, objB: obj) : bool
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
Multiple items
type Foo =
  new : unit -> Foo

Full name: Script.Foo

--------------------
new : unit -> Foo
val x : Foo

Full name: Script.x
module Unchecked

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

Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
val z : Foo

Full name: Script.z
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/3Y
Posted:13 years ago
Author:fholm
Tags: nullable