Slider with Unitful.jl values

I know I am being dumb here… If I use a Slider from PlutoUI I cannot get it to work with Unitful.jl types.

@bind mass Slider(1u"kg":100u"kg", show_value=true)

DimensionError: kg and 1 are not dimensionally compatible.

  1. uconvert(::Unitful.FreeUnits{(kg,), 𝐌, nothing}, ::Int64)@conversion.jl:80
  2. (::Colon)(::Unitful.Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}}, ::Unitful.Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})@range.jl:134
  3. macro expansion@Local: 2277 [inlined]
  4. top-level scope@Local: 1

That specific error is coming from 1u"kg":100u"kg". In constructing the range from the colon syntax there’s an implicit step size of 1 (dimensionless) that it’s trying to materialize. Instead, you could try constructing the range object without the : syntax:

julia> range(; start = 1u"kg", stop = 100u"kg", step = 1u"kg")
(1:100) kg

Thankyou! I did not realise a step was involved.
This also works:
@bind mass Slider(1u"kg":1u"kg":100u"kg", show_value=true)

Another way to write this:

julia> (1:100).*u"kg"
(1:100) kg
2 Likes