Creating a specialisation of a built-in type?

Hi,
I want to work through the “Ray Tracer Challenge” book whilst learning julia.

To do so I’d like to achieve the following:

  • have a MyTuple type that acts exactly as regular tuples do
  • be able to redefine operators for MyTuple as needed, i.e. have Base.(==)(a::MyTuple, b::MyTuple) that acts a little different from regular == as motivated by the book

In other words, unless I define specialized operators and methods for MyTuple, I want all built-in operators and methods for Tuple to be used for MyTuple.

How would you achieve that in julia?

Basically: encapsulation. Create a struct containing your concrete Tuple type as a member, and then forward the methods you want to apply directly to the inner tuple. You can use @forward from Lazy.jl to make that easier.

There is no way to automatically apply “all” functions that act on Tuple to your own type–you will need to explicitly @forward or implement the ones you want. This sounds inconvenient, but I suspect there are relatively few methods you’ll actually need (getindex and size might be enough).

1 Like