juliadynamics / mixedstructtypes.jl Goto Github PK
View Code? Open in Web Editor NEWEasy-to-use sum types in Julia
License: MIT License
Easy-to-use sum types in Julia
License: MIT License
E.g. say we have
using MixedStructTypes
abstract type AbstractShape end
@sum_structs Shape <: AbstractShape begin
@kwdef struct Square
side::Float64 = rand()
end
@kwdef struct Rectangle
width::Float64 = rand()
height::Float64 = rand()
end
@kwdef struct Triangle
base::Float64 = rand()
height::Float64 = rand()
end
@kwdef struct Circle
radius::Float64 = rand()
end
end
currently we can define an area
function like so:
function area(sh)
if kindof(sh) === :Square
area_square(sh)
elseif kindof(sh) === :Rectangle
area_rectangle(sh)
elseif kindof(sh) === :Triangle
area_triangle(sh)
elseif kindof(sh) === :Circle
area_circle(sh)
else
area_default(sh)
end
end
area_square(s) = s.side * s.side
area_rectangle(r) = r.width * r.height
area_triangle(t) = 1.0/2.0 * t.base * t.height
area_circle(c) = π * c.radius^2
area_default(::AbstractShape) = 0.0
but actually we can generate all of this. So probably something like this should suffice
@dispatch area(s::Square) = s.side * s.side
@dispatch area(r::Rectangle) = r.width * r.height
@dispatch area(t::Triangle) = 1.0/2.0 * t.base * t.height
@dispatch area(c::Circle) = π * c.radius^2
@dispatch area(::AbstractShape) = 0.0
e.g. in the example in the readme, let's say only B
has a special value
@dispatch value(::B) = 2
then it would be useful if this would also work to reduce verbosity:
@dispatch value(::A) = 1
which would mean that all other kinds have that value
Not sure if you were aware, didn't find any caveat mentioned.
I'm trying to incorporate @pattern
in a package, and it works great, but loading extensions now fails. Offending line is
DynamicSumTypes.jl/src/Pattern.jl
Line 64 in 5dc034e
ERROR: LoadError: InitError: Evaluation into the closed module `FooModule` breaks incremental
compilation because the side effects will not be permanent. This is likely due to some other
module mutating `FooModule` with `eval` during precompilation - don't do this.
This would be breaking but it seems a better syntax which would allow more flexibility, it is also used in https://github.com/fredrikekre/EnumX.jl
Strangely it fails only on 1.8 with the struct_sum_type macro, it seems a convert problem in SumTypes.jl
CI run: https://github.com/JuliaDynamics/MixedStructTypes.jl/actions/runs/7647801175/job/20839415780
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
Unfortunately last pr #20 is not good enough
This can be done with an adaptation of the technique discussed in https://discourse.julialang.org/t/how-not-to-commit-type-piracy-when-handling-automatic-conversion-for-field-with-union-type/109877/5, thanks to @abraemer for the help
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.