Stream: helpdesk (published)

Topic: one(x) != oneunit(x) where typeof(x) <: Integer


view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 20:41):

Does anyone know of an example when one(x) does not equal oneunit(x) where x <: Integer?

view this post on Zulip Mason Protter (Apr 13 2021 at 20:44):

If the compiler supported Wrapper{T} <: T, then unitful integers could be the canonical example

view this post on Zulip Mason Protter (Apr 13 2021 at 20:44):

But we don't allow it :cry:

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 20:44):

I'm trying to sort out the comments in https://github.com/JuliaLang/julia/pull/39808 , and I'm not sure if there is a concrete scenario where one or oneunit would make a difference.

view this post on Zulip Mosè Giordano (Apr 13 2021 at 21:16):

I guess a custom type T<:Integer could in principle define one(::T) and oneunit(::T) to be different?

view this post on Zulip Mason Protter (Apr 13 2021 at 21:28):

For example, one could imagine a scenario where a Dates.jl package treated dates and years and such as subtypes of Integer

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 22:15):

julia> struct Quantum{T <: Integer} <: Integer
           val::T
       end

julia> Base.one(x::Quantum{T}) where T = one(x.val)

julia> Base.float(x::Quantum{T}) where T = float(x.val)

julia> q = Quantum(5)
Quantum{Int64}(5)

julia> one(q)
1

julia> oneunit(q)
Quantum{Int64}(1)

Last updated: Dec 28 2024 at 04:38 UTC