Skip to content

allow recursive sum types #19

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

Merged
merged 4 commits into from
Apr 2, 2023
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SumTypes"
uuid = "8e1ec7a9-0e02-4297-b0fe-6433085c89f2"
authors = ["MasonProtter <[email protected]>"]
version = "0.3.6"
version = "0.3.7"

[deps]
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ end

```
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 76.941 μs … 285.838 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 83.523 μs ┊ GC (median): 0.00%
Time (mean ± σ): 89.009 μs ± 17.598 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
Range (min … max): 61.309 μs … 83.300 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 62.350 μs ┊ GC (median): 0.00%
Time (mean ± σ): 62.376 μs ± 528.152 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

▆ ▂█▄▂ ▃▃▂
██▆████▆▅███▇██▇▆█▇▇▇▇█▇▇▆█▇▇▇█▇▆▆▆▆▇▅▇▇▆▆▆▆▆▆▆▅▆▆▅▅▆▅▅▅▅▆▅▅ █
76.9 μs Histogram: log(frequency) by time 167 μs <
▃█▂ ▁▄▃▂
▂▁▁▁▁▁▁▁▁▂▁▂▃▅▅▇███▆▄▃▃▄▄▇████▅▄▃▂▂▂▁▂▂▂▁▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
61.3 μs Histogram: frequency by time 64 μs <

Memory estimate: 0 bytes, allocs estimate: 0.
```
Expand Down Expand Up @@ -434,7 +434,8 @@ BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Memory estimate: 0 bytes, allocs estimate: 0.
```

Unityper.jl SumTypes.jl is slightly slower in this benckmark, though there are others where it is faster. SumTypes.jl has some advantages relative to Unityper.jl too, such as:
SumTypes.jl is able to slightly beat Unityper.jl in this benckmark, though there are cases where the roles are reversed.
SumTypes.jl has some other advantages relative to Unityper.jl too, such as:
- SumTypes.jl allows [parametric types](https://docs.julialang.org/en/v1/manual/types/#Parametric-Types) for much greater container flexibility (Unityper does some memory layout optimizations that won't work with parametric types).
- SumTypes.jl does not require default values for every field of the struct
- SumTypes.jl's `@cases` macro is more powerful and flexible than Unityper's `@compactified`.
Expand Down
24 changes: 15 additions & 9 deletions src/SumTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ const unsafe = Unsafe()

struct Uninit end

struct Singleton{name} end
Base.iterate(x::Singleton, s = 1) = nothing
maybe_type(::Type{x}) where {x} = x
maybe_type(::Singleton{x}) where {x} = Singleton{x}

const tag = Symbol("#tag#")
get_tag(x) =getfield(x, tag)
get_tag_sym(x::T) where {T} = tags_flags_nt(T)[get_tag(x)]
# get_tag(x::T) where {T} = getfield(x, tag)
struct Variant{fieldnames, Tup <: Tuple}
data::Tup
Variant{fieldnames, Tup}(::Unsafe) where {fieldnames, Tup} = new{fieldnames, Tup}()
# Variant(::Unsafe, nt::NamedTuple{names, Tup}) where {names, Tup} = new{fieldnames, Tup}(Tuple(nt))
# Variant{fieldnames}(t::Tup) where {fieldnames, Tup <: Tuple} = new{fieldnames, Tup}(t)
Variant{fieldnames, Tup}(t::Tuple) where {fieldnames, Tup <: Tuple} = new{fieldnames, Tup}(t)
end
Base.:(==)(v1::Variant, v2::Variant) = v1.data == v2.data

Base.iterate(x::Variant, s = 1) = iterate(x.data, s)
Base.indexed_iterate(x::Variant, i::Int, state=1) = (Base.@_inline_meta; (getfield(x.data, i), i+1))

const tag = Symbol("#tag#")
get_tag(x) = getfield(x, tag)
get_tag_sym(x::T) where {T} = keys(tags_flags_nt(T))[Int(get_tag(x))]

show_sumtype(io::IO, m::MIME, x) = show_sumtype(io, x)
function show_sumtype(io::IO, x::T) where {T}
tag = get_tag(x)
sym = flag_to_symbol(T, tag)
if getfield(x, sym) isa Singleton
if getfield(x, sym) isa Variant{(), Tuple{}}
print(io, String(sym), "::", typeof(x))
else
print(io, String(sym), '(', join((repr(data) for data ∈ getfield(x, sym)), ", "), ")::", typeof(x))
Expand Down
2 changes: 1 addition & 1 deletion src/cases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ macro cases(to_match, block)

ex = :(if $get_tag($data) === $symbol_to_flag($Typ, $(QuoteNode(stmts[1].variant)));
$(stmts[1].iscall ? :(($(stmts[1].fieldnames...),) =
$getfield($data, $(QuoteNode(stmts[1].variant))) :: $constructor($Typ, $Val{$(QuoteNode(stmts[1].variant))} )) : nothing);
$getfield($data, $(QuoteNode(stmts[1].variant))) :: $constructor($Typ, $Val{$(QuoteNode(stmts[1].variant))} ) ) : nothing);
$(stmts[1].rhs)
end)
Base.remove_linenums!(ex)
Expand Down
Loading