8 people like it.
Like the snippet!
Implicit conversion to discriminated union
SRTP and active patterns tricks for mimicking Typescript union types
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
|
// Caution!
// Don't use in real code.
module Domain =
type UnionType =
| Int of int
| Long of int64
| String of string
with
static member ($) (UnionType, x:int) = Int(x)
static member ($) (UnionType, x:int64) = Long(x)
static member ($) (UnionType, x:string) = String(x)
let inline (|UnionType|) x = Unchecked.defaultof<UnionType> $ x
open Domain
let show x =
match x with
| Int x -> printfn "int: %d" x
| Long x -> printfn "long: %d" x
| String x -> printfn "string: %s" x
let inline showImplicit (UnionType x) = show x
showImplicit "Hello world!"
|
type UnionType =
| Int of int
| Long of int64
| String of string
static member ( $ ) : UnionType:'a * x:int -> UnionType
static member ( $ ) : UnionType:'a * x:int64 -> UnionType
static member ( $ ) : UnionType:'a * x:string -> UnionType
Full name: Script.Domain.UnionType
union case UnionType.Int: int -> UnionType
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
union case UnionType.Long: int64 -> UnionType
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int64
--------------------
type int64 = System.Int64
Full name: Microsoft.FSharp.Core.int64
--------------------
type int64<'Measure> = int64
Full name: Microsoft.FSharp.Core.int64<_>
Multiple items
union case UnionType.String: string -> UnionType
--------------------
module String
from Microsoft.FSharp.Core
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
Multiple items
val UnionType : 'a
--------------------
type UnionType =
| Int of int
| Long of int64
| String of string
static member ( $ ) : UnionType:'a * x:int -> UnionType
static member ( $ ) : UnionType:'a * x:int64 -> UnionType
static member ( $ ) : UnionType:'a * x:string -> UnionType
Full name: Script.Domain.UnionType
val x : int
val x : int64
val x : string
val x : 'a (requires member ( $ ))
module Unchecked
from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T
Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
module Domain
from Script
val show : x:UnionType -> unit
Full name: Script.show
val x : UnionType
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val showImplicit : 'a -> unit (requires member ( $ ))
Full name: Script.showImplicit
Multiple items
active recognizer UnionType: 'a -> '_arg3
Full name: Script.Domain.( |UnionType| )
--------------------
type UnionType =
| Int of int
| Long of int64
| String of string
static member ( $ ) : UnionType:'a * x:int -> UnionType
static member ( $ ) : UnionType:'a * x:int64 -> UnionType
static member ( $ ) : UnionType:'a * x:string -> UnionType
Full name: Script.Domain.UnionType
More information