This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Data: sprintf() with overloaded objects

=head1 VERSION

  Maintainer: Ilya Zakharevich <[EMAIL PROTECTED]>
  Date: 15 September 2000
  Mailing List: [EMAIL PROTECTED]
  Number: 235
  Version: 1
  Status: Developing

=head1 ABSTRACT

This RFC proposes a support of sprintf() with overloaded objects;

=head1 DESCRIPTION

The problems:

=over

=item *

too many different flavors to support, many of them equivalent;

=item *

Many different flags, a lot of work to implement them all.

=item *

C<*> in the flags.

=back

One would want an interface which allows to get acceptable results with
a minimal of work, but also allows as fine control as possible if needed.
The solution:

=over

=item *

merge many flavors into groups, with one overloaded operation per group.
For example, C<'%i'> and C<'%d'> should invoke the same overloaded operation
keyed by C<%d> (or C<%?d>, see below).

=item *

provide the actual letter (and flags!) as an argument to disambiguate it
inside the group;

=item *

provide I<two> operations to overload per group (only one of them should
be used), for example C<'%+040d'> should invoke the operation keyed by
C<'%?d'> if present, otherwise one keyed by C<'%d'>, then would postprocess
the results.  Similarly, C<'%d'> should invoke the operation keyed by
C<'%d'> if present, otherwise one keyed by C<'%d'>.

=item *

If C<*> is present in the flags, the count is provided as an additional
argument to the corresponding method.  (One additional argument per each C<*>.)

=back

For example, if method C<'%d'> is overloaded by the method C<FORMAT_D>,
and C<'%?d'> is not overloaded, then

  sprintf('%s %0*i %s', '--', $overloaded_object, 10, '--')

calls

  $overloaded_object->FORMAT_D('0*i', 10);

and postprocesses the result.

=head1 Composite objects

The "standard" semantic of processing flags in the overloaded methods for
composite objects (such as complex numbers or vectors) should be:
delegate the flags to components "as is".  (As opposed to applying widths
to the I<total> field.)

There may be obvious exceptions: for complex numbers C<+>/C<-> before the
imaginary part is needed, but if one inserts this sign no matter what,
the format C<'%+10i'> would result in a double C<+> sign or C<+-> or C<-->.
The other approach of prepending C<'+'> to the flags no matter what would
omit space for the format C<'% 10i'>.

=head1 MIGRATION ISSUES

NONE

=head1 IMPLEMENTATION

More or less straightforward

=head1 REFERENCES

RFC ???

Reply via email to