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? thanks! -- ---- peace, sergio photographer, journalist, visionary http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101 |
On 2012-09-11, at 20:36, sergio_101 <[hidden email]> 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. 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 :) |
In reply to this post by sergio_101
Hmm... there's no "magic" for this.
Some options: Instead of instantiating your objects with new, try defining your own class method for getting a new instance. Something like: Dog class>>newWithParams: aDictionary ^self new params: aDictionary Dog>>params: aDictionary params := aDictionary
Is that something similar to what you were looking for? Cheers, Carla.
On Tue, Sep 11, 2012 at 3:36 PM, sergio_101 <[hidden email]> wrote: yes, i know this sounds simple.. but is it possible to instantiate an |
In reply to this post by Camillo Bruni-3
On Sep 11, 2012, at 8:41 PM, Camillo Bruni wrote:
yourself. Otherwise you may have some strange behavior if the #initializeWithSound: message doesn't return the receiver :) Ben
|
On 2012-09-11, at 20:50, Benjamin <[hidden email]> wrote:
> On Sep 11, 2012, at 8:41 PM, Camillo Bruni wrote: >> On 2012-09-11, at 20:36, sergio_101 <[hidden email]> 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 :) well initialize.* usually returns self :) (it's like #initialize, where you never send #yourself) |
In reply to this post by Carla F. Griggio
> Dog class>>newWithParams: aDictionary
> ^self new params: aDictionary > > Dog>>params: aDictionary > params := aDictionary > > Is that something similar to what you were looking for? > yes! this is very close to what i am looking to do.. this is kind of where i figured it was going to go.. i was hoping it would be a one liner.. but i can live with this.. thanks! -- ---- peace, sergio photographer, journalist, visionary http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101 |
If you actually wanted to just set the sound of the dog, and you don't really need a Dictionary, you can do:
myDog := Dog new sound: 'bark' Or if you wanted to use the dictionary, same thing:
myDog := Dog new params: params. But I don't really know what were you using that dictionary for and what was the intention (#params: is not a very nice name). As Camillo said, (almost) everything is a message send, you just have to decide if you want a class message to create an 'initialized' dog all at once, or if using regular setters is enough. Cheers! Carla.
On Tue, Sep 11, 2012 at 3:53 PM, sergio_101 <[hidden email]> wrote:
|
In reply to this post by sergio_101
In Smalltalk you could do that, with any class side constructor or with a builder class.
If you're looking for something more similar to how some JavaScript constructors work, where you can map property names of an anonymous object to properties of the new instance then there is no such feature, but I guess you could build it more or less easily passing a dictionary as argument. Ie. JavaScript: Object.create(Dog.prototype, { sound: 'bark', pitch: 1.1, filterFunction: function(s){...} }) And then have a Dog instance with sound, pitch and filterFunction copied from the second argument. Ie. Smalltalk Dog fromDictionary: (Dictionary new at: #sound put: 'bark'; at: #pitch put: 1.1; at: #filterFunction put: [:s | ]; yourself). And then map the keys in the dictionary to methods, properties or instance variables of your new Dog instance. Kind of hacky for Smalltalk, but useful in JS due to its prototypical essence. Not having a hash/dictionary literal syntax makes its use counter-intuitive and too verbose. I'd rather use my own constructor, maybe it will take a little longer but in the long term the explicit selector will make its semantics more clear. Regards, -- Esteban. |
In reply to this post by Camillo Bruni-3
On Sep 11, 2012, at 8:52 PM, Camillo Bruni wrote: > On 2012-09-11, at 20:50, Benjamin <[hidden email]> wrote: >> On Sep 11, 2012, at 8:41 PM, Camillo Bruni wrote: >>> On 2012-09-11, at 20:36, sergio_101 <[hidden email]> 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 :) > well initialize.* usually returns self :) (it's like #initialize, where you never send #yourself) Play with Morphic and then you will take this habit too :) Ben |
In reply to this post by Carla F. Griggio
>
> But I don't really know what were you using that dictionary for and what was > the intention (#params: is not a very nice name). > i have been intentionally being vague about what i am doing, as i am trying to understand the best smalltalk practice, without introducing an approach from another language. the idea i am after is something like this in rails.. i can make a hash like: dog_params = {:sound => 'bark', :food => 'steak'} and create a dog like: Dog.create(dog_params) and have a dog that with those params.. i just want to make sure i am not inventing a problem to solve.. and that there isn't a more 'smalltalky' way to do it. thanks! -- ---- peace, sergio photographer, journalist, visionary http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101 |
On 09/11/2012 12:28 PM, sergio_101 wrote:
> i have been intentionally being vague about what i am doing, as i am > trying to understand the best smalltalk practice, without introducing > an approach from another language. This is a good guide: http://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X |
>
> This is a good guide: > > http://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X > i actually just bought that, and am making my way through it now.. thanks! -- ---- peace, sergio photographer, journalist, visionary http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101 |
In reply to this post by sergio_101
There is no other way to do what you want except using a dictionary/collection/etc. They keywords in Smalltalk must preserve the order because they're part of the method name, not its arguments. You could build your own way of passing "dynamic" parameters to the constructor, but most of the times if you need too many arguments for a constructor you might be needing to introduce a new abstraction, so take it as a sign to reify something. Syntax sugar in Smalltalk most of the times comes from better abstractions, not from the language's syntax. Regards, -- Esteban. |
In reply to this post by sergio_101
This small (really small and useful) book could help you with your style
On Tue, Sep 11, 2012 at 4:52 PM, sergio_101 <[hidden email]> wrote:
Bernardo E.C. |
In reply to this post by Esteban A. Maringolo
>
> You could build your own way of passing "dynamic" parameters to the > constructor, but most of the times if you need too many arguments for a > constructor you might be needing to introduce a new abstraction, so take it > as a sign to reify something. thanks, esteban.. this is exactly where my sixth sense was taking me.. -- ---- peace, sergio photographer, journalist, visionary http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101 |
In reply to this post by sergio_101
read Smalltalk by Example book of alec sharp
Stef On Sep 11, 2012, at 8:36 PM, sergio_101 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? > > thanks! > > > -- > ---- > peace, > sergio > photographer, journalist, visionary > > http://www.ThoseOptimizeGuys.com > http://www.CodingForHire.com > http://www.coffee-black.com > http://www.painlessfrugality.com > http://www.twitter.com/sergio_101 > http://www.facebook.com/sergio101 > |
Free forum by Nabble | Edit this page |