[% setvar title Data: overloading via the SECOND operand if needed %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

Data: overloading via the SECOND operand if needed

VERSION

  Maintainer: Ilya Zakharevich <ilya@math.ohio-state.edu>
  Date: 15 Sep 2000
  Mailing List: perl6-language-data@perl.org
  Number: 234
  Version: 1
  Status: Developing

ABSTRACT

This RFC proposes a support of a situation when a more-knowledgable module may steal overloading from a less-knowledgable module or visa versa;

DESCRIPTION

The problem: if both arguments to a binary operation are overloaded objects, then with the current implementation the overloaded method registered for the first argument is loaded.

Sometimes it leads to problems: for example, Math::BigFloat objects support Math::BigInt objects as the other arguments for binary operations, but not visa versa, so

   print $bigfloat + $bigint;		# Works
   print $bigint   + $bigfloat;		# Does not

Solution: provide a pragma to inform the overloading mechanism that Math::BigFloat is more versatile than Math::BigInt

   use overload ':override', 'Math::BigFloat', 'Math::BigInt';

This would make

    $bigint * $bigfloat

call

    $bigfloat->MULTIPLY($bigint, 1)

instead of

    $bigint->MULTIPLY($bigfloat, 0)

(assuming that the MULTIPLY methods are registered for '*' key).

This requires additional costly checks from the overloading engine. However, it is not hard to implement this with the following features:

Given this, the additional overhead of the hash lookup incurs only if needed, this may be ignored.

Overriding does not chain automatically, but if :override_chain is used instead of :override, the overrider will also override packages overriden by the package it overrides.

A method to query overriding info should be provided.

If A isa B, and B overrides C, then A overrides C. Similarly, if C isa D, then A overrides D.

MIGRATION ISSUES

None.

IMPLEMENTATION

Keep a global flag whether override was ever requested. Keep a flag in each package whether it is overrided by anything. Keep a hash of packages which the given package overrides. Keep these data in the overloading table of the package.

REFERENCES

None.