Skip to content

Commit

Permalink
Merge pull request #53 from edgarfgp/unify-unions
Browse files Browse the repository at this point in the history
Simplify union definitions
  • Loading branch information
edgarfgp committed Feb 1, 2024
2 parents d59d6da + 5852e2d commit 06a0948
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 350 deletions.
1 change: 0 additions & 1 deletion src/Fabulous.AST.Tests/Fabulous.AST.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<Compile Include="Members\InterfaceMember.fs" />
<Compile Include="Members\AbstractMember.fs" />
<Compile Include="TypeDefinitions\Union.fs" />
<Compile Include="TypeDefinitions\GenericUnion.fs" />
<Compile Include="TypeDefinitions\Abbrev.fs" />
<Compile Include="TypeDefinitions\UnitsOfMeasure.fs" />
<Compile Include="TypeDefinitions\Enum.fs" />
Expand Down
108 changes: 0 additions & 108 deletions src/Fabulous.AST.Tests/TypeDefinitions/GenericUnion.fs

This file was deleted.

99 changes: 99 additions & 0 deletions src/Fabulous.AST.Tests/TypeDefinitions/Union.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,102 @@ type Colors = | Red
[<Test>]
type Colors = | [<Obsolete; Test>] Red
"""

module GenericUnion =

[<Test>]
let ``Produces an union with TypeParams`` () =
AnonymousModule() {
GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkLongIdent("'other"))
}

UnionCase("Green")
UnionCase("Blue")
UnionCase("Yellow")
}
}

|> produces
"""
type Colors<'other> =
| Red of a: string * b: 'other
| Green
| Blue
| Yellow
"""

[<Test>]
let ``Produces an union with TypeParams and interface member`` () =
AnonymousModule() {
Interface("IMyInterface") {
let parameters = [ CommonType.Unit ]
AbstractCurriedMethodMember("GetValue", parameters, CommonType.String)
}

(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkLongIdent("'other"))
}

UnionCase("Green")
UnionCase("Blue")
UnionCase("Yellow")
})
.members() {
let expr = Expr.Constant(Constant.FromText(SingleTextNode("\"\"", Range.Zero)))

InterfaceMember(CommonType.mkLongIdent("IMyInterface")) {
MethodMember("x.GetValue") { EscapeHatch(expr) }
}
}
}

|> produces
"""
type IMyInterface =
abstract member GetValue: unit -> string
type Colors<'other> =
| Red of a: string * b: 'other
| Green
| Blue
| Yellow
interface IMyInterface with
member x.GetValue() = ""
"""

[<Test>]
let ``Produces an struct union with TypeParams`` () =
AnonymousModule() {
(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkLongIdent("'other"))
}

UnionCase("Green")
UnionCase("Blue")
UnionCase("Yellow")
})
.attributes([ "Struct" ])

}

|> produces
"""
[<Struct>]
type Colors<'other> =
| Red of a: string * b: 'other
| Green
| Blue
| Yellow
"""
1 change: 0 additions & 1 deletion src/Fabulous.AST/Fabulous.AST.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<Compile Include="Widgets\TypeDefinitions\Abbrev.fs" />
<Compile Include="Widgets\TypeDefinitions\UnitsOfMeasure.fs" />
<Compile Include="Widgets\TypeDefinitions\Union.fs" />
<Compile Include="Widgets\TypeDefinitions\GenericUnion.fs" />
<Compile Include="Widgets\TypeDefinitions\Record.fs" />
<Compile Include="Widgets\TypeDefinitions\Interface.fs" />
<Compile Include="Widgets\TypeDefinitions\Class.fs" />
Expand Down

0 comments on commit 06a0948

Please sign in to comment.