I watched Stephane's Squeak videos (they helped alot btw) and was left wondering how one goes about making multiple constructors for a class. Is it even possible? Thank you.
-Rich
Any class method can be a constructor.
At the end of the day (bottom of the stack) most new instances are created with basicNew. This is a low level call that just allocates the object's memory and associates it with the class (and sets all ivars to nil). The default ctor in user space is new, which calls basicNew and then initialize on the object.
Suppose you have a Socket class and want a constructor that takes an ip address. You'd just create a class side method
openOnIpAddress: anAddress ^self new openOn: anAddress.
or something like that (this is totally off the top of my head - any resemblance to actual Squeak code is purely coincidental).
-Todd Blanchard
On Thursday, May 11, 2006, at 10:05AM, Rich rjseagraves@gmail.com wrote:
I watched Stephane's Squeak videos (they helped alot btw) and was left wondering how one goes about making multiple constructors for a class. Is it even possible? Thank you.
-Rich _______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
On Thursday, May 11, 2006, at 10:05AM, Rich rjseagraves@gmail.com wrote:
I watched Stephane's Squeak videos (they helped alot btw) and was left wondering how one goes about making multiple constructors for a class. Is it even possible? Thank you.
-Rich
Things that are built into other languages are done by convention in Smalltalk. Smalltalk builds everything out of objects and messages.
In this case, there are no real constructors in Smalltalk. The things that look like constructors are just regular methods, but methods on a class instead of an instance. Smalltalkers tend to call these methods "instance creation methods" rather than constructors, but they are the messages you send to a class to create instances, so they play the same role as constructors in Java or C++.
Because instance creation methods are just methods (but on the class, instead of on the instance), you can define as many as you want. Look at class Date. There is #today, #tomorrow, #yesterday, #newDay:year:, #newDay:month:year:, #year:day: #year:month:day:
In fact, the real problem is that it is so easy to make "constructors" that people clutter up the class with a lot of them instead of figuring out a base set and sticking to it!
-Ralph Johnson
This is possible, you don't have to do anything specific other tham define those methods and call them where appropriate...
chrisp
Den May 11, 2006 kl. 7:05 PM skrev Rich:
I watched Stephane's Squeak videos (they helped alot btw) and was left wondering how one goes about making multiple constructors for a class. Is it even possible? Thank you.
-Rich _______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org