-
Notifications
You must be signed in to change notification settings - Fork 0
/
Option.fs
22 lines (18 loc) · 811 Bytes
/
Option.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Option
let private getOptionType t =
let optionGenericType = typedefof<FSharp.Core.option<_>>
optionGenericType.MakeGenericType([|t|])
let toOptionNone t =
// remark could be replaced with
// toOptionNone t :obj = null
let optionType = getOptionType t
let cases = FSharp.Reflection.FSharpType.GetUnionCases(optionType)
let noneFsharpType = cases |> Seq.find(fun i -> i.Name = "None")
FSharp.Reflection.FSharpValue.MakeUnion(noneFsharpType, [||])
let toOptionSomeFromType t v =
let optionType = getOptionType (t)
let cases = FSharp.Reflection.FSharpType.GetUnionCases(optionType)
let someFsharpType = cases |> Seq.find(fun i -> i.Name = "Some")
FSharp.Reflection.FSharpValue.MakeUnion(someFsharpType, [|v|])
let toOptionSome v =
toOptionSomeFromType (v.GetType()) v