contextExtensions.pl - Implements a framework for creating contexts that extend other contexts.
MathObject contexts specify their features by creating objects that implement the needed functionality, and assigning those object classes to the various operators, functions, etc. that are part of the context. For example, addition in the Numeric
context is attached to the +
sign by setting its class
property to Parser::BOP::add
in the context's operators
list.
To change the action of +
(for example, to allow it to work with a new type of object that you are defining), you would change the class
property to point to a new class (usually a subclass of Parser::BOP::add
) that implements the new functionality needed for the new category of object. So if you are defining a new object to handle quaternions, then you might use something like
$context->operators->set( '+' => 'context::Quaternions::BOP::add' );
to direct the +
to use your new context::Quaternions::BOP::add
object instead of the usual one. (Of course, there is much more than needs to be done as well, but this illustrates how such changes are made.)
When you change the class associated with an operator or some other Context feature, the previous class is replaced by the new class, and that means you have to either maintain the old functionality by using a subclass of the original class, or by re-implementing it in your new class. This usually means you need to know the original class when you define your new objects, and that makes your new context dependent on a specific original context. If you want to be able to add your new MathObject to an arbitrary context, that was not generally easy to do.
The purpose of this file is to make it possible to overcome these difficulties, and make it easier to extend a context by adding new functionality without losing its old features, and without having to know which context you are extending. For example, the Fraction object can be added to an existing context this way, as can the handling of units.