gmail got confused and remove Mason's actual question, to which I was responding:
I'd like to write a library that includes functions that can operate on user-defined types and delegate part of their implementation to user-defined functions. For example: zero!(x) = (x[1:end] = 0) where `x` could be a built-in array, a user-defined GPU-memory array, a user-defined sparse array, or some other wacky type the library writers don't know about. In Python this is simple: every object carries around a reference to its class' capabilities. The only way I can think to mimic this behavior in Julia at the moment is by asking the user to `include` the library (which is not scalable), defining macros instead of functions (which means users have to remember which library exports are polymorphic) or overloading the `[]` operator for attribute access like PyCall (which seems obfuscated). Any suggestions? On Mon, Apr 7, 2014 at 9:03 PM, Jameson Nash <[email protected]> wrote: > Generally, you would define an abstract type that the user would > inherit. Inheriting from an abstract type is essentially a declaration > that the child class will provide definitions for some > functions/behavior > > You would also define prototypes for any functions the user code is > expected to implement (c.f. Base.Graphics) > > You can use a type union over abstract and concrete types to include > limit the applicable types of the function you are defining (zero!, in > your example), or just leave it completely up to duck-typing (as you > currently are doing in your example) > > (switching to julia-users since this is mainly a question on how to > use the language, not a design proposal for consideration, and > julia-dev is basically a subset of julia-users anyways) > > > On Mon, Apr 7, 2014 at 8:30 PM, Mason McGill <[email protected]> wrote: >> To clarify, this is in julia-dev rather than julia-users because it doesn't >> seem there's a Julia analogue to features in other languages like >> interfaces, concepts, or duck typing that allow user code and library code >> to collaborate. Was this a conscious design decision? Is anything in the >> works to improve the state of generic library writing in Julia? Is there a >> solution I don't know about?
