Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify union definitions #53

Merged
merged 1 commit into from
Feb 1, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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