Wrap and inherit Number

Very cool.

I’d like to add some clarity as well:
The “thing” that would be most useful (and maybe I should open a new topic for this), is if we could just have something that acts exactly like a Number (in every meaning), but that we can override some of the functions that already accept it as an argument.

So here’s an instance of Float64:
Float64(33.0)
We can do all sorts of things with it. Here is an instance of a custom type A:
A(33.0)
We can do exactly the same things like the Float64 version from before. Now I change the existing sin function to the following (silly):
sin(x::A) = 2x.
There. Instances of A do everything, say, floats can do, and I modified the behavior of a few select functions. Winning.

1 Like

How would you address https://github.com/JuliaLang/julia/issues/9821#issuecomment-70381499?

I liked the idea of TypedDelegation.jl very much, will these macros be in Base by any chance at some point?

Almost certainly not, since they are unlikely to be needed for anything in Base.

Thank you @stevengj, good to know.

It’s not totally true, see e.g length, size etc. for Generator in generator.jl. Some time ago I started a branch with this delegate functionality, but thought I indeed needed to find more cases to push that into Base! In my case, I needed indirection for the iteration protocol (similar to the iter function in python).

Sorry for bumping this rather old post. But, any updates on this direction? I try to define a set of structs representing different kinds of prices (e.g. OpenPrice, ClosePrice, etc.), which shall behave like a Float but use its type name to do multiple dispatch.

2 Likes