Switching Application Contexts When Invoking a Method

When an imported method is invoked on an instance of an imported class or a class that implements an imported interface, the app, global, and currentSchema system variables switch to being those of the package. The package designer can rely on the appropriate application object being used in methods, depending on the context of the method.

This default rule for the objects referenced by the app, global, and currentSchema system variables when methods are invoked is exactly what is required in most situations. However, an extension to the normal syntax for invoking a method enables you to force a switch to a specific set of app, global, and currentSchema objects.

// Normal syntax, which uses the default app, global, and currentSchema
obj.meth(params);

// Extension to use app, global, and currentSchema for a specific context
obj.meth(params) in (application context reference);

In the extended syntax, parentheses are required around the method parameters even if there are no actual parameters.

The compiler ensures that the parameters for the method to be invoked have the correct type and are in the correct order.

Parentheses are required around the application context reference unless it is a simple name.

The application context is obtained from the appContext system variable. In the following example, the importing schema obtains the application context of the package.

vars
    package : PackageClass;          // An imported class
    context : ApplicationContext;
begin
    create package transient;
    context := package.getContext;   // Could save this value for future use
epilog
    delete package;
end;

The getContext method returns the value of the appContext system variable within the package.

getContext(): ApplicationContext;
begin
    return appContext;
end;

In the following example, a method written in the importing schema executes a method to write currentSchema.name, but by forcing a switch to the application context of the package, it is the name of the package schema that is output.

vars
    package : PackageClass;
begin
    create package transient;
    writeSchemaName;                           // Importing schema name
    writeSchemaName() in (package.getContext); // Package schema name
epilog
    delete package;
end;