On Thu, 10 Oct 2002, Michael Lazzaro wrote: : 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".
That's why it's short. :-) : 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(). Sorry, no. The bare class name will refer to the class itself in such constructs as when Dog { bark() } when Cat { meow() } if $! =~ IntegerOverflow {...} &squeek := Mouse sub ($x) { return mickey($x) } : (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.) Yep, I know about those. Can't say I wasn't tempted. But the simpler concept should generally have the simpler usage, I think, all other things being equal. If you're going to do something verbish with a noun, there ought to be a verb. That being said, () is a verb of sorts, so we could allow a constructor syntax in which a class functions as a subroutine reference. But that'd probably be a shorthand for .new(), or whatever the conversion method is called, where conversion from undef is equivalent to an argumentless .new(). : 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... Note that according to the current design team notions your syntax there is declaring the type of the variable (like a tie), not the type of the value. You want either my MyClass $obj; or my $obj returns MyClass; for that. (The first is just synactic sugar for the second.) Things get more complicated for arrays and hashes. The following are equivalent: my MyClass @array; my @array returns MyClass; my @array is Array of MyClass; Or something like that. How the constructor syntax will fit in with that is still up in the air. I'm rather partial to tacking a closure on the end like sub syntax, only the closure executes immediately at elaboration time with the topic being the current object. But that may be the general syntax rather than the usual syntax. Possibly a declaration can serve as a topicalizer for any assignment or binding to itself, so maybe you could say my MyClass $obj = .new; to mean the same as my MyClass $obj { .new } or maybe my MyClass $obj { .init } Or maybe we can have something weird like my MyClass.new $obj; That would assume that the object you're creating functions as its own class. Except you don't actually have the object at compile time... Perhaps one of my new MyClass $obj; new my MyClass $obj; new MyClass my $obj; my MyClass $obj is init; my MyClass $obj.init; could also be allowed for an argumentless form, though. But this is all wild speculation at this point. Larry