It seems to me that there are several advantages to making a group of multi with the same short name a single object, of type MultiSub|MultiMethod, which internally holds references to the all the various routines that share that short name.

It would behave like a tied sub (or method), with a .dispatch method to decide which of the contained routines should be called this particular time. Manhattan would be the default. However, one can override the dispatch logic; implementing Luke's Patterns idea, for example.

This would also provide a relatively sane way to take a reference to a single contained routine. I would expect a .matching method which takes a list of types, and returns a reference to which routine would be called given parameters of those types. Or one could get a reference to the entire multi via C< my $ref = &multi; >. I don't think there has been syntax introduced thus far that enables this ability. But it's likely I missed it along the way, like I seem to be missing several other things lately.

Another thing I haven't seen is how to declare a new multi at runtime. Non-multi's can be created via C< &func := sub {...}; >, but that's destructive to any other routines that might have occupied that namespace. Using the object model, one could do this with C< &func.add( sub {...}); >. There could also be a .delete method to remove a routine already in the multi (give it a reference to uniquely identify it). Of course, Luke would need to override the .add method to one that doesn't throw errors when more than one routine with the same type signature is thrown.

If this happens, I would expect that C< multi > would become a macro which creates the multi of that short name if it doesn't already exist, and then perform an .add to that multi.



There are likely several rough edges in here, and I've by no means convinced myself that this is the Right Thing, but I thought I'd throw the idea out there to what others thought of it.

-- Rod Adams.



Reply via email to