@@ -50,22 +50,14 @@ let private transformBaseConsCall com ctx r (baseEnt: FSharpEntity) (baseCons: F
50
50
| e -> e
51
51
52
52
let private transformNewUnion com ctx r fsType ( unionCase : FSharpUnionCase ) ( argExprs : Fable.Expr list ) =
53
- match fsType, unionCase with
54
- | ErasedUnionCase ->
55
- Fable.NewTuple argExprs |> makeValue r
56
- | ErasedUnion( tdef, _ genArgs, rule) ->
57
- match argExprs with
58
- | [] -> transformStringEnum rule unionCase
59
- | [ argExpr] -> argExpr
60
- | _ when tdef.UnionCases.Count > 1 ->
61
- " Erased unions with multiple cases must have one single field: " + ( getFsTypeFullName fsType)
62
- |> addErrorAndReturnNull com ctx.InlinePath r
63
- | argExprs -> Fable.NewTuple argExprs |> makeValue r
64
- | StringEnum( tdef, rule) ->
65
- match argExprs with
66
- | [] -> transformStringEnum rule unionCase
67
- | _ -> sprintf " StringEnum types cannot have fields: %O " tdef.TryFullName
68
- |> addErrorAndReturnNull com ctx.InlinePath r
53
+ match com, fsType, unionCase with
54
+ | ErasedUnion( kind, tdef, _ genArgs) ->
55
+ match kind, argExprs with
56
+ | EraseKind.AsNamedTuple caseRule, [] -> transformStringEnum caseRule unionCase
57
+ | EraseKind.AsNamedTuple _, _ -> ( makeStrConst unionCase.Name):: argExprs |> Fable.NewTuple |> makeValue r
58
+ | EraseKind.AsValue, [ arg] -> arg
59
+ | EraseKind.AsValue, _ -> failwith " Shouldn't happen, error?"
60
+ | EraseKind.AsTuple, _ -> Fable.NewTuple argExprs |> makeValue r
69
61
| OptionUnion typ ->
70
62
let typ = makeType ctx.GenericArgs typ
71
63
let expr =
@@ -228,14 +220,16 @@ let private transformUnionCaseTest (com: IFableCompiler) (ctx: Context) r
228
220
unionExpr fsType ( unionCase : FSharpUnionCase ) =
229
221
trampoline {
230
222
let! unionExpr = transformExpr com ctx unionExpr
231
- match fsType, unionCase with
232
- | ErasedUnionCase ->
233
- return " Cannot test erased union cases"
234
- |> addErrorAndReturnNull com ctx.InlinePath r
235
- | ErasedUnion( tdef, genArgs, rule) ->
236
- match unionCase.UnionCaseFields.Count with
237
- | 0 -> return makeEqOp r unionExpr ( transformStringEnum rule unionCase) BinaryEqualStrict
238
- | 1 ->
223
+ match com, fsType, unionCase with
224
+ | ErasedUnion( kind, tdef, genArgs) ->
225
+ match kind with
226
+ | EraseKind.AsNamedTuple caseRule ->
227
+ if unionCase.UnionCaseFields.Count = 0 then
228
+ return makeEqOp r unionExpr ( transformStringEnum caseRule unionCase) BinaryEqualStrict
229
+ else
230
+ let name = Fable.Get( unionExpr, Fable.TupleIndex( 0 ), Fable.String, None)
231
+ return makeEqOp r name ( makeStrConst unionCase.Name) BinaryEqualStrict
232
+ | EraseKind.AsValue ->
239
233
let fi = unionCase.UnionCaseFields.[ 0 ]
240
234
let typ =
241
235
if fi.FieldType.IsGenericParameter then
@@ -247,17 +241,15 @@ let private transformUnionCaseTest (com: IFableCompiler) (ctx: Context) r
247
241
else fi.FieldType
248
242
let kind = makeType ctx.GenericArgs typ |> Fable.TypeTest
249
243
return Fable.Test( unionExpr, kind, r)
250
- | _ ->
251
- return " Erased unions with multiple cases cannot have more than one field: " + ( getFsTypeFullName fsType )
244
+ | EraseKind.AsTuple ->
245
+ return " Cannot test erased union cases"
252
246
|> addErrorAndReturnNull com ctx.InlinePath r
253
247
| OptionUnion _ ->
254
248
let kind = Fable.OptionTest( unionCase.Name <> " None" && unionCase.Name <> " ValueNone" )
255
249
return Fable.Test( unionExpr, kind, r)
256
250
| ListUnion _ ->
257
251
let kind = Fable.ListTest( unionCase.CompiledName <> " Empty" )
258
252
return Fable.Test( unionExpr, kind, r)
259
- | StringEnum(_, rule) ->
260
- return makeEqOp r unionExpr ( transformStringEnum rule unionCase) BinaryEqualStrict
261
253
| DiscriminatedUnion( tdef,_) ->
262
254
let tag = unionCaseTag tdef unionCase
263
255
return Fable.Test( unionExpr, Fable.UnionCaseTest( tag), r)
@@ -678,18 +670,19 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
678
670
| BasicPatterns.UnionCaseGet ( unionExpr, fsType, unionCase, field) ->
679
671
let r = makeRangeFrom fsExpr
680
672
let! unionExpr = transformExpr com ctx unionExpr
681
- match fsType, unionCase with
682
- | ErasedUnionCase ->
683
- let index = unionCase.UnionCaseFields |> Seq.findIndex ( fun x -> x.Name = field.Name)
684
- return Fable.Get( unionExpr, Fable.TupleIndex( index), makeType ctx.GenericArgs fsType, r)
685
- | ErasedUnion _ ->
686
- if unionCase.UnionCaseFields.Count = 1 then return unionExpr
687
- else
673
+ match com, fsType, unionCase with
674
+ | ErasedUnion( kind, _, _) ->
675
+ let getByIndex offset =
688
676
let index = unionCase.UnionCaseFields |> Seq.findIndex ( fun x -> x.Name = field.Name)
689
- return Fable.Get( unionExpr, Fable.TupleIndex index, makeType ctx.GenericArgs fsType, r)
690
- | StringEnum _ ->
691
- return " StringEnum types cannot have fields"
692
- |> addErrorAndReturnNull com ctx.InlinePath r
677
+ Fable.Get( unionExpr, Fable.TupleIndex( index + offset), makeType ctx.GenericArgs fsType, r)
678
+ match kind with
679
+ | EraseKind.AsValue -> return unionExpr
680
+ | EraseKind.AsTuple -> return getByIndex 0
681
+ | EraseKind.AsNamedTuple _ ->
682
+ if unionCase.UnionCaseFields.Count = 0 then
683
+ return " StringEnum types cannot have fields" |> addErrorAndReturnNull com ctx.InlinePath r
684
+ else
685
+ return getByIndex 1
693
686
| OptionUnion t ->
694
687
return Fable.Get( unionExpr, Fable.OptionValue, makeType ctx.GenericArgs t, r)
695
688
| ListUnion t ->
0 commit comments