Support mixin of structs
See original GitHub issueHi @essenciary, @abhimanyuaryan quite some time that the mixin topic was raised in StippleUI. Now I have a new use case in StipplePlotly to follow this up.
I propose to include the following in Stipple.
macro mixin(expr, prefix = "", postfix = "")
x = eval(expr)
pre = eval(prefix)
post = eval(postfix)
T = x isa DataType ? x : typeof(x)
mix = x isa DataType ? x() : x
values = [Stipple.Observables.to_value(getfield(mix, f)) for f in fieldnames(T)]
output = quote end
for (f, type, v) in zip(Symbol.(pre, fieldnames(T), post), fieldtypes(T), values)
push!(output.args, :($(esc(f))::$type = $v) )
end
:($output)
end
and the following in StipplePlotly
const PlotlyEvent = Dict{String, Any}
@kwdef struct PlotlyEvents
_selected::R{PlotlyEvent} = PlotlyEvent()
_hover::R{PlotlyEvent} = PlotlyEvent()
_click::R{PlotlyEvent} = PlotlyEvent()
_relayout::R{PlotlyEvent} = PlotlyEvent()
end
in order to support
using Stipple
using PlotlyBase
julia> @reactive! mutable struct PlotDemo <: ReactiveModel
plot::R{Plot} = Plot()
@mixin PlotlyEvents "plot"
end
:PlotDemo
julia> fieldnames(PlotDemo)
(:plot, :plot_selected, :plot_hover, :plot_click, :plot_relayout, :channel__, :isready, :isprocessing)
I see some advanteage over the approach of Mixers.jl
as there we cannot determine the position of the fields in the model, whereas here we can, and we can easily determine pre- and postfixes.
What do you think?
Issue Analytics
- State:
- Created a year ago
- Comments:18 (6 by maintainers)
Top Results From Across the Web
Struct composition with mixin and templates - Stack Overflow
I can compose an AB struct that has all the members of structs A and B : template AFields() {int a;} struct A...
Read more >mixin and @include - Sass
Mixins allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes like...
Read more >Interfaces, Mixins and Building Powerful Custom Data ...
One way to achieve modularity and reusability in object-oriented programming is through a concept called a mixin. Different languages implement ...
Read more >MixIn Structure | Microsoft Learn
A type derived from the Implements structure. MixInType. A base type. hasImplements true if MixInType is derived from the current implementation ...
Read more >Documentation - Mixins - TypeScript
TypeScript's best mixin support is done via the class expression pattern. You can read more about how this pattern works in JavaScript here....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I know where it comes from, it’s from the fact the the macro evaluates the expression within its name space. Let me work on it a bit…
StipplePlotly also has a new version with mixin support.