How can I define isapprox() for a type such that I can set a default tolerance but the user can override it if required?
struct MyType
v::Float64
end
Base.isapprox(x::MyType, y::MyType) = isapprox(x.v, y.v, atol=10e-6)
x = MyType(10.0 / 3)
y = MyType(3.33)
@show isapprox(x, y) # usually false with 10e-6
@show isapprox(x, y, atol = 10e-12) # can be true if required
I think it’s something with kwargs ? but I forget how it’s done.
Ah, I misunderstood. You can capture a specific kwarg like this for example:
function Base.isapprox(x::MyType, y::MyType; atol=nothing, kwargs...)
if atol === nothing
atol = 1e-6
end
return isapprox(x.v, y.v; atol=atol, kwargs...)
end