JuliaGizmos / Observables.jl

observable refs
Other
104 stars 31 forks source link

type declaration wierdness #113

Open bjarthur opened 5 months ago

bjarthur commented 5 months ago

i believe this is a bug, but declaring types like this is not something i do often.

julia> using GLMakie

julia> x = Observable(1)
Observable(1)

julia> y::Observable{Union{Int,AbstractFloat}} = lift(x) do x
           x==1 ? 2 : NaN
       end
Observable(2)
    0 => (sc::Observables.SetindexCallback)(x) @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:148

julia> y[]
2

julia> x[]=3    ### y can not be set to NaN via the lift
ERROR: InexactError: Int64(NaN)
Stacktrace:
  [1] Int64
    @ ./float.jl:912 [inlined]
  [2] convert(::Type{Int64}, x::Float64)
    @ Base ./number.jl:7
  [3] setproperty!(x::Observable{Int64}, f::Symbol, v::Float64)
    @ Base ./Base.jl:40
  [4] setindex!(observable::Observable, val::Any)
    @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:122
  [5] (::Observables.MapCallback)(value::Any)
    @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:436
  [6] #invokelatest#2
    @ ./essentials.jl:892 [inlined]
  [7] invokelatest
    @ ./essentials.jl:889 [inlined]
  [8] notify
    @ ~/.julia/packages/Observables/YdEbO/src/Observables.jl:206 [inlined]
  [9] setindex!(observable::Observable, val::Any)
    @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:123
 [10] top-level scope
    @ REPL[7]:1

julia> y[]=NaN    ### but can be set to NaN directly
NaN

the workaround, which is more verbose, is:

julia> X = Observable(1)
Observable(1)

julia> Y = Observable{Union{Int,AbstractFloat}}(2)
Observable{Union{Int64, AbstractFloat}}(2)

julia> on(X) do X
       Y[] = X==1 ? 2 : NaN
       end
ObserverFunction defined at REPL[13]:2 operating on Observable(1)

julia> Y[]
2

julia> X[]=3
3

julia> Y[]
NaN

julia> X[]=1
1

julia> Y[]
2

let me know if i should have rather filed an issue with Makie.

julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 12 × Apple M2 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_PROJECT = @.
  JULIA_EDITOR = vi

(jl_PdQxLH) pkg> st
Status `/private/var/folders/s5/8d629n5d7nsf37f60_91wzr40000gq/T/jl_PdQxLH/Project.toml`
  [e9467ef8] GLMakie v0.9.9
MasonProtter commented 1 month ago

Yeah, I find this really annoying too.