Skip to content

Commit

Permalink
Fix construction with no parameters (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Aug 29, 2024
1 parent 9232b93 commit 7ec2841
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
31 changes: 10 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ like so:
```julia
julia> using DynamicSumTypes

julia> abstract type AbstractAT end
julia> abstract type AbstractS end

julia> struct A{X}
x::X
end

julia> mutable struct B
y::Float64
y::Int
end

julia> @sumtype AT(A{Int},B) <: AbstractAT
AT
julia> @sumtype S{X}(A{X},B) <: AbstractS
```

## Construction
Expand All @@ -43,21 +42,11 @@ Then constructing instances is just a matter of enclosing the type constructed i
predefined sum type:

```julia
julia> a = AT(A(1))
AT(A{Int64}(1))
julia> a = S(A(1))
S{Int64}(A{Int64}(1))

julia> b = AT(B(1.0))
AT(B(1.0))
```

Or an alternative syntax can also be used:

```julia
julia> a = AT'.A(1)
AT(A{Int64}(1))

julia> b = AT'.B(1.0)
AT(B(1.0))
julia> b = S{Int}(B(1))
S{Int64}(B(1))
```

## Access and Mutation
Expand All @@ -68,8 +57,8 @@ This works like if they were normal Julia types:
julia> a.x
1

julia> b.y = 3.0
3.0
julia> b.y = 3
3
```

## Dispatch
Expand All @@ -78,7 +67,7 @@ For this, you can simply access the variant
inside the sum type and then dispatch on it:

```julia
julia> f(x::AT) = f(variant(x))
julia> f(x::S) = f(variant(x))

julia> f(x::A) = 1

Expand Down
14 changes: 12 additions & 2 deletions src/DynamicSumTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ macro sumtype(typedef)
end
end

constructors = [:(@inline $(namify(type))(v::Union{$(variants...)}) where {$(typeparams...)} =
$(branchs(variants, variants_with_P, :(return new{$(typeparams...)}(v)))...))]

if type isa Expr
push!(
constructors,
:(@inline $type(v::Union{$(variants...)}) where {$(typeparams...)} =
$(branchs(variants, variants_with_P, :(return new{$(typeparams...)}(v)))...))
)
end

esc(quote
struct $type <: $(abstract_type)
variants::Union{$(variants...)}
@inline $type(v::Union{$(variants...)}) where {$(typeparams...)} =
$(branchs(variants, variants_with_P, :(return new{$(typeparams...)}(v)))...)
$(constructors...)
end
@inline function $Base.getproperty(sumt::$typename, s::Symbol)
v = $DynamicSumTypes.unwrap(sumt)
Expand Down
3 changes: 2 additions & 1 deletion test/sumtype_macro_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ struct None end
@test allvariants(Simple) == allvariants(typeof(b)) == (SimpleA = SimpleA, SimpleB = SimpleB)

option_none = Option{Int}(None())
option_some = Option{Int}(Some(1))
option_some = Option(Some(1))
option_some2 = Option{Int}(Some(1))
@test variant(option_none) isa None
@test variant(option_some) isa Some{Int}
@test allvariants(Option) == (None = None, Some = Some)
Expand Down

0 comments on commit 7ec2841

Please sign in to comment.