3 people like it.

Make option type usable in C#

Using FSharpOption is painful from C#, but it doesn't have to.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
module FSharpOption =
  open System.Runtime.InteropServices
  let IsSome o =
    match o with
    | Some _ -> true
    | None -> false
  let IsNone o = not <| IsSome o
  let TryGetValue o ([<Out>] value: byref<'t>) =
    match o with
    | Some v -> 
      value <- v
      true
    | None ->
      value <- Unchecked.defaultof<'t>
      false
namespace System
namespace System.Runtime
namespace System.Runtime.InteropServices
val IsSome : o:'a option -> bool

Full name: Script.FSharpOption.IsSome
val o : 'a option
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val IsNone : o:'a option -> bool

Full name: Script.FSharpOption.IsNone
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val TryGetValue : o:'t option -> value:byref<'t> -> bool

Full name: Script.FSharpOption.TryGetValue
val o : 't option
Multiple items
type OutAttribute =
  inherit Attribute
  new : unit -> OutAttribute

Full name: System.Runtime.InteropServices.OutAttribute

--------------------
OutAttribute() : unit
val value : byref<'t>
type byref<'T> = (# "<Common IL Type Omitted>" #)

Full name: Microsoft.FSharp.Core.byref<_>
val v : 't
module Unchecked

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

Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
Raw view Test code New version

More information

Link:http://fssnip.net/so
Posted:8 years ago
Author:Gauthier Segay
Tags: c# , dotnet , interop