This problem is coming up in my own library. I’m trying to implement a simple TimeRecord object, which simply adds a unix timestamp to a value:
struct TimeRecord{T}
t :: Float64
v :: T
end
A lot of times, I would simply want to do an operation on the value and pass it back and re-add the timestamp
f(x::TimeRecord) = TimeRecord(x.t, f(x.v))
In other cases for an operator for two time records, I’d want to use the following check to ensure the timestamps are equal
op(x::TimeRecord,y::TimeRecord) = x.t != y.t ? error("Timestamps must mach") : TimeRecord(x.t, op(x.v, y.v))
It just comes to a point where all I want would be something where I can say “If this function “f(x)” doesn’t have a method defined for TimeRecord{T} but has a method for T, then use TimeRecord(x.t, f(x.v))”. If this is a bad idea, at least having formal interfaces I can iterate over in a macro would help.