-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathVariantCoercion.res
122 lines (90 loc) · 3.22 KB
/
VariantCoercion.res
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
type a = One | @as("two") Two | Three
let a: a = Three
let b = (a :> string)
type onlyInts = | @as(1) One1 | @as(2) Two2 | @as(3) Three3
let i = One1
let d = (i :> int)
type onlyFloats = | @as(1.1) Onef | @as(2.2) Twof | @as(3.3) Threef
let ii = Onef
let dd = (ii :> float)
module CoerceVariants = {
@unboxed type a = One(int) | @as(1.1) Two | @as(null) T2
@unboxed type b = One(int) | @as(1.1) Two | @as(null) T2 | Three
let a: a = Two
let b: b = (a :> b)
@tag("kind") type x = One({age: int, name?: string})
@tag("kind") type y = One({age: int, name?: string}) | Two({two: string})
let x: x = One({age: 1})
let y: y = (x :> y)
}
module CoerceWithPayload = {
@unboxed type strings = String(string) | First | Second | Third
let a: strings = String("hello")
let aa: strings = First
let b: string = (a :> string)
let bb: string = (aa :> string)
@unboxed type floats = Number(float) | @as(1.) First | @as(2.) Second | @as(3.) Third
let c: floats = Number(100.)
let cc: floats = Second
let d: float = (c :> float)
let dd: float = (cc :> float)
}
module CoerceFromStringToVariant = {
@unboxed type strings = String(string) | First | Second | Third
let a = "hello"
let aa = "First"
let b: strings = (a :> strings)
let bb: strings = (aa :> strings)
@unboxed type mixed = String(string) | @as(1) One | @as(null) Null | Two
let c = "Hi"
let cc: mixed = (c :> mixed)
}
module CoerceFromIntToVariant = {
@unboxed type ints = Int(int) | @as(1) First | @as(2) Second | @as(3) Third
let a = 100
let aa = 1
let b: ints = (a :> ints)
let bb: ints = (aa :> ints)
@unboxed type mixed = Int(int) | @as(1) One | @as(null) Null | Two
let c = 120
let cc: mixed = (c :> mixed)
}
module CoerceFromFloatToVariant = {
@unboxed type floats = Float(float) | @as(1.) First | @as(2.) Second | @as(3.) Third
let a = 100.
let aa = 1.
let b: floats = (a :> floats)
let bb: floats = (aa :> floats)
@unboxed type mixed = Float(float) | @as(1.) One | @as(null) Null | Two
let c = 120.
let cc: mixed = (c :> mixed)
}
module CoerceFromBigintToVariant = {
@unboxed type bigints = BigInt(bigint) | @as(1n) First | @as(2n) Second | @as(3n) Third
let a = 100n
let aa = 1n
let b: bigints = (a :> bigints)
let bb: bigints = (aa :> bigints)
@unboxed type mixed = BigInt(bigint) | @as(1n) One | @as(null) Null | Two
let c = 120n
let cc: mixed = (c :> mixed)
}
module CoerceFromPolyvariantToVariant = {
type simple = [#One | #Two]
type simpleP = One | Two
let simple: simple = #One
let simpleP = (simple :> simpleP)
type withAs = [#One | #two]
type withAsP = One | @as("two") Two
let withAs: withAs = #One
let withAsP = (withAs :> withAsP)
type withMoreVariantConstructors = [#One | #two]
type withMoreVariantConstructorsP = One | @as("two") Two | Three
let withMoreVariantConstructors: withMoreVariantConstructors = #One
let withMoreVariantConstructorsP = (withMoreVariantConstructors :> withMoreVariantConstructorsP)
type withUnboxedCatchAll = [#One | #someOtherThing]
@unboxed
type withUnboxedCatchAllP = One | @as("two") Two | Three | Other(string)
let withUnboxedCatchAll: withUnboxedCatchAll = #One
let withUnboxedCatchAllP = (withUnboxedCatchAll :> withUnboxedCatchAllP)
}