develooper Front page | | Postings from September 2000

RFC 235 (v1) Data: sprintf() with overloaded objects

Perl6 RFC Librarian
September 15, 2000 12:25
RFC 235 (v1) Data: sprintf() with overloaded objects
Message ID:
This and other RFCs are available on the web at

=head1 TITLE

Data: sprintf() with overloaded objects

=head1 VERSION

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


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


The problems:


=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.


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:


=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<*>.)


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, '--')


  $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'>.




More or less straightforward


RFC ??? Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About