Does anyone know of an example when one(x)
does not equal oneunit(x)
where x <: Integer
?
If the compiler supported Wrapper{T} <: T
, then unitful integers could be the canonical example
But we don't allow it :cry:
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.
I guess a custom type T<:Integer
could in principle define one(::T)
and oneunit(::T)
to be different?
For example, one could imagine a scenario where a Dates.jl package treated dates and years and such as subtypes of Integer
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