I am creating a numeric type that contains a single number field, for example:
struct T
v::Float64
T(v) = 0 <= v <= 1 ? new(v) : error("invalid v")
end
In case you are wondering, I am doing this to ensure that all variables of type T
have a value between 0 and 1 (or some other invariant I might want to enforce).
Now I want to define all numeric operations on T
, simply by calling the operation on the field v
and returning a number, like:
Base.:(+)(x::T, y::T) = x.p + y.p
Is there a concise way of doing this for all numeric operations? I would also like to cover numeric functions. Maybe a macro?
I also tried
Base.convert(::Type{F}, p::T) where {F <: Number} = convert(F, p.p)
in the hope that my type would get promoted automatically to a number and then the numeric operations would work, but this does not work either.