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

[email protected] (Michael Fowler)
Newsgroups perl.perl6.language.objects
Message-ID <[email protected]>
On Fri, Sep 01, 2000 at 10:46:50AM +0100, Piers Cawley wrote:
> Are you wilfully misunderstanding me?

I would appreciate you giving me the benefit of the doubt that I'm not
intentially misunderstanding you. 


> Let's try another possible example to see if you get the point:
> 
>     print "What sort of dog do you want? ";
>     my $dog_type = ucfirst(<STDIN>); chomp $dog_type;
> 
>     try {
>         my Dog $puppy = "Dog::${dog_type}"->new(name => $dog_name);
>     }
>     catch Exception::BadType {
>         croak "Dog::${dog_type} is not a valid breed\";
>     }
>     catch {
>         croak $!;
>     }
> 
> I want 'my Dog $spot' to enforce the condition that:
> 
>     (!defined($spot) || $spot->isa('Dog'))

Yes, I had gathered this.  I tried to tell you that's not what I'm
proposing.

 
> And that is all. I *really* don't the idea of implicit object
> creation. Consider other OO languages; C++ has implicit object
> construction, but has added the explicit and implicit keywords to
> enable the programmer to turn the behaviour off. Not having actually
> programmed in C++ I don't know how often you want to do this, but the
> fact that the functionality is informative. In Java, on the other
> hand, the designers looked at C++ and decided *not* to do implicit
> construction. 

This is not how I understand C++.  Granted, I've only done a few programs in
it, but there is no implicit keyword (at least, not mentioned in the /C++
Primer/ second edition), and the explicit keyword has to do with implicit
type conversions.

In C++ the code:

    string foo;

makes foo a string object right then and there, calling the constructor
string::string().  explicit will not affect this.  Taking an example from my
book, given the class definition:

    class Number {
    public:
        explicit Number(const SmallInt &);
    };


The code:

    void func(Number);

    SmallInt si(47);

    func(si);

will cause an error at compile-time, because si cannot be successfully
converted to a Number; the explicit keyword prevents the appropriate
constructor from participating in an implicit conversion.

my string $foo is the C++ equivalent of string foo; it instantiates a
string object in $foo.


> But $spot could easily be a Dalmation object that you are using like a
> Dog in the current context. Maybe I should just write a counter
> proposal along the lines of:
> 
> =head1 TITLE
> 
> C<my Dog $spot> should simply assert that
> C<(!defined($spot) || $spot-E<gt>isa('Dog'))>
> 
> And let them duke it out.

By all means, please do so.

Declaring $spot is a Dog (without turning it into a Dog object) and the
changes I propose are not mutually exclusive unless you want to use the same
syntax.  We could use something like:

    my $spot isa(Foo);

But, whatever.


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.