How to put a block into an Array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to put a block into an Array

David Finlayson-4
Newbie here, working my way through Guzdial's Squeak: Object-Oriented
Design with Multimedia Applications (2001). One of the exercises
requires building an Array with a block of code as an element, the
idea is to lookup the stored block in some way and execute it. But,
how do you store a block of code in an Array?

For example, I can do this:

[Transcript show: 'a message'] value

But I can't get this to work:

myArray := #([Transcript show: 'a message']).
(myArray at: 1) value

The reason is that (myArray at: 1) doesn't recognize the stored object
as a block. Inspecting it shows the odd symbol: #[ ??

Can someone get me back on the clue train?

Thanks,

David
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to put a block into an Array

Tapple Gao
On Fri, May 16, 2008 at 12:11:50PM -0700, David Finlayson wrote:

> Newbie here, working my way through Guzdial's Squeak: Object-Oriented
> Design with Multimedia Applications (2001). One of the exercises
> requires building an Array with a block of code as an element, the
> idea is to lookup the stored block in some way and execute it. But,
> how do you store a block of code in an Array?
>
> For example, I can do this:
>
> [Transcript show: 'a message'] value
>
> But I can't get this to work:
>
> myArray := #([Transcript show: 'a message']).
> (myArray at: 1) value
>
> The reason is that (myArray at: 1) doesn't recognize the stored object
> as a block. Inspecting it shows the odd symbol: #[ ??
>
> Can someone get me back on the clue train?

the #() syntax only supports numbers and symbols. Use the brace
syntax, or build the array using messages:

myArray := {[Transcript show: 'a message']}
myArray := Array with: [Transcript show: 'a message']

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to put a block into an Array

David Finlayson-4
Thanks, that did it. There is a nice explaination of this syntax here:

http://www.smalltalk.org/articles/article_20040920_a2.html

which I was not aware of. My Smalltalk books are dated and don't have
the Squeak extensions to the language.

David

On Fri, May 16, 2008 at 12:17 PM, Matthew Fulmer <[hidden email]> wrote:

> On Fri, May 16, 2008 at 12:11:50PM -0700, David Finlayson wrote:
>> Newbie here, working my way through Guzdial's Squeak: Object-Oriented
>> Design with Multimedia Applications (2001). One of the exercises
>> requires building an Array with a block of code as an element, the
>> idea is to lookup the stored block in some way and execute it. But,
>> how do you store a block of code in an Array?
>>
>> For example, I can do this:
>>
>> [Transcript show: 'a message'] value
>>
>> But I can't get this to work:
>>
>> myArray := #([Transcript show: 'a message']).
>> (myArray at: 1) value
>>
>> The reason is that (myArray at: 1) doesn't recognize the stored object
>> as a block. Inspecting it shows the odd symbol: #[ ??
>>
>> Can someone get me back on the clue train?
>
> the #() syntax only supports numbers and symbols. Use the brace
> syntax, or build the array using messages:
>
> myArray := {[Transcript show: 'a message']}
> myArray := Array with: [Transcript show: 'a message']
>
> --
> Matthew Fulmer -- http://mtfulmer.wordpress.com/
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>



--
David Finlayson, Ph.D.
Operational Geologist

U.S. Geological Survey
Pacific Science Center
400 Natural Bridges Drive
Santa Cruz, CA 95060, USA

Tel: 831-427-4757, Fax: 831-427-4748, E-mail: [hidden email]
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to put a block into an Array

Tapple Gao
On Fri, May 16, 2008 at 01:02:03PM -0700, David Finlayson wrote:
> Thanks, that did it. There is a nice explaination of this syntax here:
>
> http://www.smalltalk.org/articles/article_20040920_a2.html
>
> which I was not aware of. My Smalltalk books are dated and don't have
> the Squeak extensions to the language.

There are three good syntax guides at
http://squeak.org/Documentation

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners