Best List,
Im very curious to the the 'smalltalk way' to write the following code:
| shape morph | (2 atRandom == 1) ifTrue: [shape := RectangleMorph] ifFalse: [shape := EllipseMorph]. morph := shape new.
I've tried something like:
(#(EllipseMorph RectangleMorph) at: (2 atRandom)) new.
But thats too silly ? Is there some connection to get a class from a symbol ? Or is that the wrong direction ?
Thanks in advance,
- Jeroen - aspiring squeaker.
On Jun 22, 2006, at 7:07 PM, Jeroen van Hilst wrote:
I've tried something like:
(#(EllipseMorph RectangleMorph) at: (2 atRandom)) new.
But thats too silly ? Is there some connection to get a class from a symbol ? Or is that the wrong direction ?
I like your approach. I think using the #() array here will give you Symbols - but you can use a literal array with curly braces -
{EllipseMorph. RectangleMorph}
to get the actual classes. (Note the period between the array elements.) This form of literal evaluates its expressions and uses the results to build the array. it's also useful when you want to build an array out of some variables, or have some short math or something to do.
Just to mention it, you can also get to a class from a Symbol by doing something like
Smalltalk at: #EllipseMorph
if that's needed in your situation.
I don't know if you saw, but collections also respond to #atRandom directly, so you could do
{EllipseMorph. RectangleMorph} atRandom new.
Hope this helps, Benjamin Schroeder
Benjamin, {EllipseMorph. RectangleMorph} atRandom new. Is a nice solution.
Thanky you for your very helpfull answer. - Jeroen
"Benjamin Schroeder" benschroeder@acm.org wrote in message news:A9DC7832-6691-4DA5-A5A9-AB0501F96B64@acm.org...
On Jun 22, 2006, at 7:07 PM, Jeroen van Hilst wrote:
I've tried something like:
(#(EllipseMorph RectangleMorph) at: (2 atRandom)) new.
But thats too silly ? Is there some connection to get a class from a symbol ? Or is that the wrong direction ?
I like your approach. I think using the #() array here will give you Symbols - but you can use a literal array with curly braces -
{EllipseMorph. RectangleMorph}
to get the actual classes. (Note the period between the array elements.) This form of literal evaluates its expressions and uses the results to build the array. it's also useful when you want to build an array out of some variables, or have some short math or something to do.
Just to mention it, you can also get to a class from a Symbol by doing something like
Smalltalk at: #EllipseMorph
if that's needed in your situation.
I don't know if you saw, but collections also respond to #atRandom directly, so you could do
{EllipseMorph. RectangleMorph} atRandom new.
Hope this helps, Benjamin Schroeder
beginners@lists.squeakfoundation.org