Re: RFC 171 (v2) my Dog $spot should call a constructor implicitly

[email protected] (Michael Fowler)
Newsgroups perl.perl6.language.objects
Message-ID <[email protected]>
On Thu, Aug 31, 2000 at 07:56:24PM -0700, Nathan Wiger wrote:

> > So the object would be left undefined?  That means this code:
> 
> No, not at all. Remember, everything's going to inherit from CORE, at
> the very least, which would have to provide a CREATE method which just
> instantiates a simple object.

Don't you mean UNIVERSAL, or did I miss an RFC?


> So, if no Dog::CREATE exists, then $spot becomes a regular old scalar. I
> see many many uses for this. For example, maybe you have a class that you
> just want to add a STRING sub to, so it prints out nice. Forcing a CREATE
> sub to be defined here is a needless restriction.

How is a regular scalar any better than undef?  It's actually worse,
potentially creating bugs that are even more difficult to spot.

    my Dog $spot = "Spot";  # no CREATE, $spot is now just "Spot"
    $spot->type("collie");  # Spot->type("collie") !


If you meant a reference, that's even worse.  Consider the DBI class; a
user, neglecting to read the documentation, tries:

    my DBI $dbh ($database_dsn, $database_username, $database_password);
    my $sth = $dbh->prepare( ... );

$dbh was blessed into the DBI class, but as a simple scalar.  Now the
prepare method gets a scalar reference where it was expecting a hash
reference.  I know of few classes that test their object to see if it's the
proper underlying reference, and I doubt DBI is any different.  It tries to
do something like $self->{'database_username'} and blows up rather
spectacularly, and the user sees an error deep inside of DBI.

If you meant a hash reference, there's a similar problem, though you'll see
warnings instead of fatal errors.  That is, assuming DBI uses a hash
reference (which it does, last time I checked, but this is just an example).

So, DBI is forced to provide a CREATE method if bugs are to be made easy to
track.  I would much rather the my DBI $dbh blow up then and there, so the
user gets instant feedback that they shouldn't do that with DBI.

DBI is a good example in another way; DBI->connect() returns a reference
blessed into the DBI::db class.  The default CREATE, if it returns a
reference, can only think to bless it into the DBI class.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.