How to prevent type conversion for single value array?

I am trying to create a single valued array called xs in which I am later pushing in values.

xs = [x0,]

where x0 is obtained through a slider in Pluto notebook as follows.

@bind x0 Slider(0.0:0.01:0.99, show_value=true)

However, when x0 is set as 0.0, it results in the xs being created as Int64 array which raises error while pushing further values into it.

InexactError: Int64(0.09685647168069829)
    Int64@float.jl:723[inlined]
    convert@number.jl:7[inlined]
    push!(::Vector{Int64}, ::Float64)@array.jl:928
    top-level scope@Local: 10[inlined]

Shouldn’t the xs be created as Float64 array since x0 is of that type? Also, is there a way to prevent this issue?

Seems to me a bug in PlutoUI, I can reproduce this - initially the value is 0.0, but when the slider is moved away from zero and back down it switches to Int64. A simple workaround is to actually fix the type of the Array:

xs = Float64[x0, ]

EDIT: looks like this is a known issue, with the problem being on the JS side, see here: Instability in return type of PlutoUI.Slider · Issue #64 · JuliaPluto/PlutoUI.jl · GitHub

1 Like

Incidentally, there’s no need for the comma, [0.0] works. The trailing comma is something that you need for tuples, because parentheses have several meanings.

Oh, okay! I had mistook this as syntax. Thanks!

Thank you!