I am thinking of writing a type similar to those defined in NamedArrays and Images . Say I just want an essentially array with a piece of metadata, say a user-friendly name that I will write at the top of the file when I write the array to disk. (This detail is not relevant, I just come up with an example.)
So I could do
type MyNamedArray data::Array name::ASCIIString end function mywrite(f,x::MyNamedArray) write(f,x.name) write(f,x.data) end
or something, and no other behavior should be different from the behavior of the underlying array.
In my head it is “obvious” that I just want every existing function that works with arrays to work in the data field of this type. In another language, for example. Java I could just subclass Array and add name as an instance field to a subclass that will automatically maintain compatibility with all existing Array operations. But in Julia, if I try a solution like the one above, I now need to define a ton more functions, for example. as @TimHoly and 'davidavdav' did in related packages.
Of course, I know that forcing some of these functions to be written manually is useful for understanding what you haven't thought through. For example. in the MyNamedArray example MyNamedArray , one could argue by indicating that I did not define the name x::MyNamedArray * y::MyNamedArray . But what if I just don't care about it and want the code to “just work” without a lot of templates? (See, for example, a loop with characters for entering new method descriptions in NamedArrays and manually writing hundreds of lines of definitions in images . The vast majority of these definitions are a template / “obvious” definition.)
In particular, to continue the example that I gave, for MyNamedArray , the default value may be x*y no longer MyNamedArray , i.e. since each function simply uses the “inherited” behavior of applying the same function to the underlying data by default, we can simply forget the metadata about all the functions that existed before.
Notice, I find that Thomas Lycken answers insightful here , and here is the question and answers here .
The best synthesis I can come up with is "you just need to suck it and write functions, or write a macro that does it for you." If so, so be it; I'm just wondering if I am missing a better option, especially the best way to develop a solution to make it more Julian and avoid the pattern.
inheritance oop metadata julia-lang multiple-dispatch
Philip
source share