12/10/2008

Oh, so when you say 'literal' you really do mean 'literal'!

This comes from the 'hard of thinking' category.

I was trying to create my 'children' method - necessary when you are rendering subcomponents in seaside. I knew it need to return a collection, and when I looked at an example, it showed:

 ^#()


In other words, return an empty array. Obviously, it was using the array literal syntax. This, of course, it where my thinking went horribly wrong. I created my method with the following code:

 ^#(pageHeader pageFooter).


Seaside stopped working :-( It complained about a decorator method not being understood by ByteSymbol. If you are new to seaside, and you don't already know the answer, see if you can work out what I was doing wrong.

Give up? OK, the array literal syntax creates an array of literals. I know, who would have thought it! My array contained the symbols #pageHeader and #pageFooter, NOT the value of those two instance variables. Totally obvious now, but I share it in case you are ever tempted into similar thinking. And, just for your information, the correct code is:

 Array with: pageHeader with: pageFooter.


## Update: Randal has just pointed out that there is also 'curly brace' runtime Array constructor that was limited to Squeak, but is now available in Gemstone and possibly VW. The syntax is:

 {pageHeader. pageFooter}


This makes much more sense, and is a very slick way of creating arrays. Thanks Randal

1 comment:

Randal L. Schwartz said...

You can also use the runtime Array constructor, originally an extension to Squeak, but now adopted by GemStone/S (and possibly VW):

^ { pageHeader . pageFooter }