Inheritance and Overloading
Inheritance interacts with overloading in two ways. The first occurs when a handler is named as a string rather than provided as a code reference or anonymous subroutine. When named as a string, the handler is interpreted as a method, and can therefore be inherited from superclasses.
The second interaction between inheritance and overloading is
that any class derived from a overloaded class is itself subject to
that overloading. In other words, overloading is itself inherited. The
set of handlers in a class is the union of handlers of all that
class’s ancestors, recursively. If a handler can be found in several
different ancestors, the handler actually used is governed by the
usual rules for method inheritance. For example, if class Alpha inherits from classes Beta and Gamma, in that order, and class Beta overloads + with \&Beta::plus_sub, but class Gamma overloads + with the string "plus_meth", then Beta::plus_sub will be called when you try
to apply + to an Alpha object.
Since the value of the fallback key is not
a handler, its inheritance is not governed by the rules given above.
In the current implementation, the fallback value from the first overloaded
ancestor is used, but this is accidental and subject to change without
notice (well, without much notice).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access