Re: RFC 171 (v1) my Dog $spot should call a constructor implicitly
[email protected] (Hildo Biersma)
| Newsgroups | perl.perl6.language.objects |
|---|---|
| Organization | Morgan Stanley Dean Witter & Co. |
| Message-ID | <[email protected]> |
Matt Youell wrote:
>
> So perhaps sometimes in Perl we could say:
>
> my Dog $spot = undef; # Automagically knows to be a Dog ref instead
> of a Dog object because of the undef.
> if ($age > 12) {
> $spot = new Doberman();
> } else {
> $spot = new Corgi();
> }
Right now, the default behavior of perl is that un-initialized variables
are automatically undef. It would be weird to have to do explicit
assignment of an variable to say so.
I feel this discussion confuses multiple things:
- Is a scalar an object or a reference to an object?
- Must references to objects always be initialized at definition time?
If feel that a scalar, even a typed one, is a reference to an object,
not an object itself. undef is a reasonable initial value for a
reference. In fact, supporting undefined references is necessary to
build any complex data structure.
Hence, I feel automatic initialization is the wrong thing to do. If we
want a quick and easy way to initalize object references with a
default-constructed object, we should have separate syntax to do so.
What is the problem with:
my Dog $spot; # Not initialized
my Dog $spot = Corgi->new(); # Explicit construction and assignment
my Dog $spot : default; # Call default constructor
(and feel free to replace the attribute 'default' with any other you'd
like)
Hildo