On Thursday, October 10, 2002, at 11:23 AM, John Williams wrote:
> my $obj = MyClass(...);
>
> This seems to assume that objects have a default method if you treat
> them
> like a subroutine. Kinda tcl-ish, but I don't recall anything like
> this
> in the apocalypes.
>
> my $obj = MyClass;
>
> I think $obj would contain a reference to MyClass, not an instance
> of MyClass.
This is an area that's been bothering me for a while now (the Apoc's so
far don't directly address it, AFAIK.) Here's why:
Instantiating new objects is *darn* frequent in OO programming. And of
those objects, a sizable number -- possibly even a majority -- have
constructors with no required arguments, e.g. "default constructors".
And pretty much every one of those constructors is going to be named
".new".
Therefore, since creating an object using a default constructor is
really common, it should be drop-dead simple. I think there's utility
in trying to accommodate:
$obj = MyClass.new(...); # normal case
$obj = MyClass.new; # if the constructor takes no args
$obj = MyClass(...) # implied "new" w/ args
$obj = MyClass; # implied "new", no args
Since instantiating a class is MUCH more common than is passing classes
themselves around, the latter case could be "expanded" instead:
$obj = MyClass; # instantiate it, using the default constructor
$obj = MyClass.TYPE; # assign a reference to MyClass
$obj = 'MyClass'; # ... or just treat it as a string?
.... trading less code in the very common case for more code in the
not-as-common case. (It would make other things simpler, too, e.g.
transformations.) So tho I have no say in the decision, I'm REALLY
hoping that we can leave out the new().
(The idea of being able to drop the new() was stolen from languages in
which the name of the constructor is the same as the name of the class,
there is no new() at all.)
As far as the syntax for type declarations: I have less bees about
this one. Clearly,
my $obj is MyClass;
should declare a variable of type MyClass. (You're right, it shouldn't
autoviv.) So what's the simplest syntax for typing + instantiation,
together? Dunno, maybe
my $obj is MyClass .= new;
is it, but still I'm hoping for an alternative that is easier to
explain to people when I'm training them. Hmm, think, think...
MikeL