On Sep 11, 2012, at 8:41 PM, Camillo Bruni wrote:


On 2012-09-11, at 20:36, sergio_101 <sergio.rrd@gmail.com> wrote:

yes, i know this sounds simple.. but is it possible to instantiate an
object with some parameters?

say i have a dictionary:

params := Dictionary new.
params at: 'sound' put: 'bark'

dog := Dog new.

^^ at this point, i would like to just create a dog and send it
'params' all at once..

can i do this? what would the syntax look like?

sure :) in Smalltalk (almost) everything is a message-send :)

So you can do the following:

dog := Dog sound: 'wuff'.

then a class side method:

Dog class >> #sound: aString
^ self basicNew
initializeWithSound: aString;
yourself.

Otherwise you may have some strange behavior if the #initializeWithSound: message doesn't return the receiver :)


Ben



then the initializeWithSound: methods on instance side

Dog >> #initializeWithSound: aString.
self initialize.
"now do the special initialization here..."
" for instance set the sound"
sound := aString.

Dog >> initialize
"do the default initialization here"
"for instance set a default sound"
sound := 'bark'.


Hope that helps :)