2 people like it.

Nullable to Option

it's allways a pain to work with F#'s Option values outside of F# - there you've got the Nullable-class this is a short snippet to convert Nullable<'a> to 'a option

1: 
2: 
3: 
4: 
let NullableToOption (n : System.Nullable<_>) = 
   if n.HasValue 
   then Some n.Value 
   else None
val NullableToOption : n:System.Nullable<'a> -> 'a option (requires default constructor and value type and 'a :> System.ValueType)

Full name: Script.NullableToOption
val n : System.Nullable<'a> (requires default constructor and value type and 'a :> System.ValueType)
namespace System
Multiple items
type Nullable =
  static member Compare<'T> : n1:Nullable<'T> * n2:Nullable<'T> -> int
  static member Equals<'T> : n1:Nullable<'T> * n2:Nullable<'T> -> bool
  static member GetUnderlyingType : nullableType:Type -> Type

Full name: System.Nullable

--------------------
type Nullable<'T (requires default constructor and value type and 'T :> ValueType)> =
  struct
    new : value:'T -> Nullable<'T>
    member Equals : other:obj -> bool
    member GetHashCode : unit -> int
    member GetValueOrDefault : unit -> 'T + 1 overload
    member HasValue : bool
    member ToString : unit -> string
    member Value : 'T
  end

Full name: System.Nullable<_>

--------------------
System.Nullable()
System.Nullable(value: 'T) : unit
property System.Nullable.HasValue: bool
union case Option.Some: Value: 'T -> Option<'T>
property System.Nullable.Value: 'a
union case Option.None: Option<'T>
Raw view Test code New version

More information

Link:http://fssnip.net/45
Posted:12 years ago
Author:Carsten König
Tags: nullable , option