Hi,
My name is Jordi Delgado and this is my first message to this list. After reading the 'purple book', I've chosen Squeak to practice what I've learnt.
Following Guzdial's book and trying to solve exercise 8 of chapter 2 (chapter 2!!; what kind of 'introductory' book is this?), and making a long story short, I got to this piece of code:
| f | f := #('a' [Transcript show: 'An a was input']). f := (f copyFrom: 2 to: f size). (Compiler evaluate: f) class
Selecting and printing gives the following result: Squeak Image 2.5 --> BlockContext Squeak Image 3.9 --> Array
I've been playing with the method evaluate: (and variants) of the Compiler class in order to get some clue as to what is the reason of this behavior, but I guess I do not know enough Smalltalk to succeed.
Any help will be much appreciated.
Thanks in advance,
Bests,
Jordi
Hi!
Jordi Delgado jdelgado@lsi.upc.edu wrote:
I got to this piece of code:
| f | f := #('a' [Transcript show: 'An a was input']). f := (f copyFrom: 2 to: f size). (Compiler evaluate: f) class
Selecting and printing gives the following result: Squeak Image 2.5 --> BlockContext Squeak Image 3.9 --> Array
Well, I am surprised that 2.5 resulted in a BlockContext, AFAIK at least from 3.2 and onwards the expression:
#('a' [Transcript show: 'An a was input'])
results in:
#('a' #[ #Transcript #show: 'An a was input' #])
An array literal does not evaluate the different bits in it, it just assumes they are Strings or Symbols separated with spaces, even if they do not have the hashmark in front of them.
You can use the newer syntax with {} to get the Parser to actually evaluate the expressions, but then you need to separate using periods:
{'a'. [Transcript show: 'An a was input']}
Now... back to that code of yours, it is a serious mess. :)
| f | f := #('a' [Transcript show: 'An a was input']). "<- as we saw this produces an Array of Symbols in newer Squeaks" f := (f copyFrom: 2 to: f size). "<- this actually produces an Array, not a single element!" (Compiler evaluate: f) class "<- the Compiler is friendly and sends asString to its argument if it is not a String or a Stream. And sending asString to an Array gives its literal representation which we actually can evaluate - to an Array again :)."
So... I wonder what the 2.5 image does to end up with a BlockContext, don't have one handy. Perhaps it does some funny stuff if the argument to Compiler is an Array?
regards, Göran
PS. Select parts of your code and use alt-i or alt-I or simply debug it to follow it.
beginners@lists.squeakfoundation.org