Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,10 +1316,13 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) appliedGenArgs fs
| ErasedUnionCase ->
let index = unionCase.Fields |> Seq.findIndex (fun x -> x.Name = field.Name)

return Fable.Get(unionExpr, Fable.TupleIndex(index), makeType ctx.GenericArgs unionType, r)
return Fable.Get(unionExpr, Fable.TupleIndex(index), fieldType, r)
| ErasedUnion _ ->
if unionCase.Fields.Count = 1 then
return unionExpr
if unionExpr.Type = fieldType then
return unionExpr
else
return Fable.TypeCast(unionExpr, fieldType)
else
let index = unionCase.Fields |> Seq.findIndex (fun x -> x.Name = field.Name)

Expand Down
20 changes: 20 additions & 0 deletions tests/Beam/InteropTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ type ErasedUnion =
type ErasedUnionWithMultipleFields =
| ErasedUnionWithMultipleFields of string * int

type ErasedPayload = { Description: string; Membership: int }

[<Erase>]
type ErasedWrapper =
| ErasedSuccess of ErasedPayload
| ErasedFailure of string

// ============================================================
// StringEnum tests
// ============================================================
Expand Down Expand Up @@ -408,6 +415,19 @@ let ``test Erased types can have members`` () =
()
#endif

[<Fact>]
let ``test Erased union payload fields can be read`` () =
#if FABLE_COMPILER
let payload = { Description = "a"; Membership = 2 }

match ErasedSuccess payload with
| ErasedSuccess details -> details.Membership
| ErasedFailure _ -> -1
|> equal 2
#else
()
#endif

[<Fact>]
let ``test Erased unions with multiple fields work`` () =
#if FABLE_COMPILER
Expand Down
15 changes: 15 additions & 0 deletions tests/Js/Main/UnionTypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ open Fable.Core.JsInterop
#endif
type DU = Int of int | Str of string

type ErasedPayload = { Description: string; Membership: int }

[<Fable.Core.Erase>]
type ErasedWrapper =
| ErasedSuccess of ErasedPayload
| ErasedFailure of string

type T1 = T1
type T2 = T2
type T3 = T3
Expand Down Expand Up @@ -213,6 +220,14 @@ let tests =
"HELLO" |> Wrapper |> U3.Case3 |> toString |> equal "OLLEH"
#endif

testCase "Erased union payload fields can be read" <| fun () ->
let payload = { Description = "a"; Membership = 2 }

match ErasedSuccess payload with
| ErasedSuccess details -> details.Membership
| ErasedFailure _ -> -1
|> equal 2

testCase "Equality works in filter" <| fun () ->
let original = [| { Name = "1"; Case = MyUnion.Case1 } ; { Name = "2"; Case = MyUnion.Case1 }; { Name = "3"; Case = MyUnion.Case2 }; { Name = "4"; Case = MyUnion.Case3 } |]
original
Expand Down
16 changes: 16 additions & 0 deletions tests/Python/TestUnionType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ open Fable.Core
#endif
type DU = Int of int | Str of string

type ErasedPayload = { Description: string; Membership: int }

[<Fable.Core.Erase>]
type ErasedWrapper =
| ErasedSuccess of ErasedPayload
| ErasedFailure of string

[<Fact>]
let ``test Union cases matches with no arguments can be generated`` () =
let x = Male
Expand Down Expand Up @@ -200,6 +207,15 @@ let ``test Erased union type testing works`` () =
"HELLO" |> Wrapper |> U3.Case3 |> toString |> equal "OLLEH"
#endif

[<Fact>]
let ``test Erased union payload fields can be read`` () =
let payload = { Description = "a"; Membership = 2 }

match ErasedSuccess payload with
| ErasedSuccess details -> details.Membership
| ErasedFailure _ -> -1
|> equal 2

[<Fact>]
let ``test Equality works in filter`` () =
let original = [| { Name = "1"; Case = MyUnion3.Case1 } ; { Name = "2"; Case = MyUnion3.Case1 }; { Name = "3"; Case = MyUnion3.Case2 }; { Name = "4"; Case = MyUnion3.Case3 } |]
Expand Down
Loading