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

=head1 TITLE

my Dog $spot should call a constructor implicitly

=head1 VERSION

    Maintainer: Michael Fowler <[EMAIL PROTECTED]>
    Date: 29 August 2000
    Last Modified: 31 August 2000
    Mailing List: [EMAIL PROTECTED]
    Version: 2
    Number: 171
    Status: Developing

=head1 ABSTRACT

The current optimization behaviour of the syntax my Dog $spot should be
replaced with a call to some implicit constructor, which creates a Dog
object out of the $spot variable.

=head1 DESCRIPTION

What is currently an optimization for pseudo-hashes:

    my Dog $spot = Dog->new();

should be replaced with:

    my Dog $spot;

which calls an implicit constructor (discussed further in the IMPLEMENTATION
section).  The optimization behaviour can be retained in some form (see the
MIGRATION section).


This syntax can also be extended to provide a more robust constructor,
allowing the specification of arguments:

    my Dog $spot (color => "brown", name => "Spot", answers_to => "yo dog!");


=head2 Examples

This is most readable and useful when declaring types, as in:

    my int $i = 4;
    my float $f;

And could be extended into lists, arrays, and hashes:

    my int ($x, $y, $z);
    my float ($price, $tax) = (4.90, 0.04);
    my array_ref @matrix;       # list of lists
    my int %counters;           # hash of integer values, possibly optimized

=head1 IMPLEMENTATION

my Dog $spot resolves to a method call in the Dog package.  This method is
called as if it were called as a class method with an optional argument (in
the case of assignment).  For example, the syntax:

    my Dog $spot = "Spot";

would be transformed to, or be the equivalent of:

    $spot = Dog->$METHOD("Spot");

If the multi-argument form of the constructor were to be adopted this would
have to change.  Dog->$METHOD would either have to return a tied scalar,
thus being the equivalent of:

    $spot = Dog->$METHOD()->STORE("Spot");

Or RFC159 would have to be adopted, producing the equivalent:

    $spot = Dog->$METHOD();
    $spot->STORE("Spot");


And, finally, the syntax:

    my Dog $spot (@args);

would be the equivalent of:

    $spot = Dog->$METHOD(@args);



=head2 Constructor

Listed below are constructor alternatives.  If a sub is chosen (as in sub
PREPARE {}) it will suffer from the disadvantage of potentially causing
problems with current Perl modules.

If an appropriate constructor is not provided a fatal exception should be
raised when the syntax is used.

=over 4

=item sub PREPARE {} or PREPARE {}

This was the name Ilya Zakharevich and p5p decided on in a similar proposal
for Perl 5 (see the REFERENCES section).


=item sub CREATE {} or CREATE {}

As RFC 159 somewhat implies the syntax could resolve to the CREATE method.


=item sub NEW {} or NEW {}

Like CREATE, but maps a little more cleanly to the large body of modules.


=item author-defined

Allow the author to define an implicit constructor.  This would be the most
flexible method, needing only one constructor to be defined (unless
semantics differ, of course).

This definition would probably be best provided through a pragma:

    use constructor qw(new);

perhaps even dropped on top of overload:

    use overload constructor => 'new';

=back



=head1 MIGRATION

All current my Dog $spot constructs will have to be converted to simply my
$spot.

The optimization provided by the current syntax could be retained, in a
slightly different form.  If an object is constructed using my Dog $spot
compile- and run-time checks could be put in place to force the $spot object
to B<always> be blessed into the "Dog" package, raising a fatal exception if
this promise is broken in any way.



=head1 CHANGES

    version 2
        - made it a little clearer that this is to replace the current
          meaning of the syntax
        - replaced the MIGRATION section with better migration method
        - added a PREPARE constructor alternative
        - made a distinction between sub $METHOD {} and $METHOD {} in the
          constructor alternatives list
        - added a suggestion for the list argument form of the constructor,
          with some alternatives to the my Dog $spot if it's adopted
        - added some examples of what the syntax could be useful for
        - removed lvalue RFC references that shouldn't have made it into v1,
          added some p5p archive references
        - added this CHANGES section



=head1 REFERENCES

RFC 159: True Polymorphic Objects


p5p archives -- each link is the start or continuation of the discussion

my Number $n
    http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-04/msg00955.html
    http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg00479.html
    http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-10/msg00356.html

[PATCH 5.005_62] my Dog $spot and prepare
    http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-11/msg00882.html
    http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-12/msg00070.html

Reply via email to