Skip to content

@eval code generation is kind of confusing! #112

@rafaqz

Description

@rafaqz

I was trying to track some broken styling in inputs.jl, but the eval blocks make it a little hard to track what is happening! I'm wondering if it is worth having them at all, in terms of the obfuscation vs DRYness ratio they deliver.

You could get a lot off the way by just extracting a few shared functions, say in slider.jl:

slider(WT::WidgetTheme, vals::AbstractArray, formatted_vals = format.(vec(vals)); value = medianelement(vals), kwargs...) =
    build_slider(slider, WT, vals, formatted_vals, value)

rangeslider(WT::WidgetTheme, vals::AbstractArray, formatted_vals = format.(vec(vals)); value = medianelement(vals), kwargs...) =
    build_slider(rangeslider, WT, vals, formatted_vals, value)

function build_slider(f, WT, vals, formatted_vals, value)
    T = Observables._val(value) isa Vector ? Vector{eltype(vals)} : eltype(vals)
    value isa AbstractObservable || (value = Observable{T}(value))
    vals = vec(vals)
    indices = axes(vals)[1]
    f = x -> _map(t -> searchsortedfirst(vals, t), x)
    g = x -> vals[Int.(x)]
    ObservablePair(value, f = f, g = g).second

    value = build_value(vals, value)
    index = build_index(vals)

    wdg = Widget(f(WT, indices, formatted_vals; value = index, kwargs...), output = value)
    wdg["value"] = value
    wdg
end

Anyway, that is just my 2 cents! And mostly because I imagine I'll be reading this code a lot more, because its so useful :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions