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

Sum type #149

Open
jariji opened this issue Nov 4, 2022 · 1 comment
Open

Sum type #149

jariji opened this issue Nov 4, 2022 · 1 comment

Comments

@jariji
Copy link
Contributor

jariji commented Nov 4, 2022

Currently I think MLStyle makes unions but not sum types. Do you think MLStyle could make this kind of type structure? The union cannot be extended from outside, which may be appropriate sometimes.

# Credit to @MasonProtter for this code.
using MLStyle

no_constructor() = nothing

macro new(T, args...)
    esc(Expr(:new, T, args...))
end

struct Chocolate
    no_constructor()
end
struct Vanilla
    no_constructor()
end
struct Strawberry
    no_constructor()
end

struct Flavour
    x::Union{Chocolate, Vanilla, Strawberry}
end
Chocolate() = Flavour(@new Chocolate)
Vanilla() = Flavour(@new Vanilla)
Strawberry() = Flavour(@new Strawberry)

MLStyle.@as_record Flavour

cc @MasonProtter

@jariji jariji changed the title Disjoint union/sum type Sum type Nov 4, 2022
@thautwarm
Copy link
Owner

The issue can be very general to discuss about. I'd recommend you the Scala's approach to sum types, and I think in this approach, we can treat a concrete type in Julia as a constructor of some sum type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants