question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support mixin of structs

See original GitHub issue

Hi @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:closed
  • Created a year ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
hhaenselcommented, Jun 21, 2022

@hhaensel I get the error #119 on

julia> PlotlyEvents
PlotlyEvents

julia> Stipple.@mixin PlotlyEvents
ERROR: LoadError: UndefVarError: PlotlyEvents not defined
Stacktrace:
 [1] top-level scope
   @ :0
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] eval(x::Symbol)
   @ Stipple ~/.julia/dev/Stipple/src/Stipple.jl:16
 [4] var"@mixin"(__source__::LineNumberNode, __module__::Module, expr::Any, prefix::Any, postfix::Any) (repeats 2 times)
   @ Stipple ~/.julia/dev/Stipple/src/Stipple.jl:1312
in expression starting at REPL[9]:1

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…

1reaction
hhaenselcommented, Jun 22, 2022

StipplePlotly also has a new version with mixin support.

@reactive! mutable struct PlotDemo <: ReactiveModel
    @mixin plot::PlotWithEvents
end

@reactive! mutable struct PBPlotDemo <: ReactiveModel
    @mixin plot::PBPlotWithEvents
end
julia> fieldnames(PlotDemo)
(:plot_data, :plot_layout, :plot_selected, :plot_hover, :plot_click, :plot_relayout, :channel__, :isready, :isprocessing)

julia> fieldnames(PBPlotDemo)
(:plot, :plot_selected, :plot_hover, :plot_click, :plot_relayout, :channel__, :isready, :isprocessing)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found