Description
First: awesome package! I really really hope Julia incorporates traits natively some day.
I was wondering what the best way is to use @traitfn
on functions in Base?
For example, in DynamicQuantities.jl, I am trying to define a IsQuantity{X}
trait rather than having to use <: AbstractQuantity
: SymbolicML/DynamicQuantities.jl#24. This is so that I can have types be subtyped on AbstractArray
, while still working with the functions in my library if they are quantity-like.
However, I quickly realized that using @traitfn
on Base can invalidate methods. For example:
julia> using SimpleTraits
julia> struct MyFloat
x::Float64
end
julia> @traitdef IsMyFloat{X}
julia> @traitimpl IsMyFloat{MyFloat}
julia> @traitfn Base.:+(l::T, r::T) where {T; IsMyFloat{T}} = T(l.x + r.x)
julia> MyFloat(0.1) + MyFloat(0.2)
MyFloat(0.30000000000000004)
This works, but invalidates any generic methods in Base for +
because it expands to:
Base.:+(l::T, r::T) where {T}
I'm assuming not, but I just wanted to hear if you know any way around this? Maybe the short answer is to avoid using @traitfn
on functions in Base...
Cheers,
Miles