[Newbies] Multiple constructors
johnson at cs.uiuc.edu
Fri May 12 10:29:23 UTC 2006
On Thursday, May 11, 2006, at 10:05AM, Rich <rjseagraves at 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.
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!
More information about the Beginners