OK, I'm beginning to think that I'm missing the obvious. As mentioned in a previous email I'm working through Guzdial's _Squeak OODMA_ and to be more specific I'm working on Exercise 8 of Chapter 2 on page 47. I'm asked to implement a method switchOn that can be used like
#( ('a' [Transcript show: 'An a was input']) ('b' [Transcript show: 'A b was input']) ) switchOn 'a'.
I believe that the Array literal above should result in an Array of 2 elements, each element being an Array of 2 elements (a String and a BlockContext). But... What I end up with is an Array of 2 elements, each element being an Array of 6 (yes six) elements. To be most specific when I inspect element 1 of the outer Array I get an array composed of
1: 'a' 2: #[ 3: #Transcript 4: #show: 5: 'An a was input' 6: #]
Is this correct behaviour?
Ken
On Saturday 12 January 2002 03:06 pm, Ken Causey wrote:
#( ('a' [Transcript show: 'An a was input']) ('b' [Transcript show: 'A b was input']) ) switchOn 'a'.
1: 'a' 2: #[ 3: #Transcript 4: #show: 5: 'An a was input' 6: #]
Is this correct behaviour?
Yes (but it is different than VW). Try instead:
{ { 'a' . [Transcript show: 'An a was input'] } . { 'b' . [Transcript show: 'A b was input'] } } switchOn 'a'.
or just use the existing method caseOf: (which takes an array of Block->Block associations).
Ken Causey ken@ineffable.com wrote...
OK, I'm beginning to think that I'm missing the obvious. As mentioned in a previous email I'm working through Guzdial's _Squeak OODMA_ and to be more specific I'm working on Exercise 8 of Chapter 2 on page 47. I'm asked to implement a method switchOn that can be used like
#( ('a' [Transcript show: 'An a was input']) ('b' [Transcript show: 'A b was input']) ) switchOn 'a'.
I believe that the Array literal above should result in an Array of 2 elements, each element being an Array of 2 elements (a String and a BlockContext). But... What I end up with is an Array of 2 elements, each element being an Array of 6 (yes six) elements. To be most specific when I inspect element 1 of the outer Array I get an array composed of
1: 'a' 2: #[ 3: #Transcript 4: #show: 5: 'An a was input' 6: #]
Is this correct behaviour?
Yes. Blocks have never been recognized within literal arrays in Squeak.
If you want something that looks like this, and that works, use curly braces instead of parentheses.
{ {'a'. [Transcript show: 'An a was input']}. {'b'. [Transcript show: 'A b was input']} } switchOn 'a'.
The {} construct expects a series of expressions (which can be blocks or anything) separated by '.'.
If you really want to execute the three lines as you gave them, then you'll have to write a recognizer for brackets, and separately ask the compiler to evaluate what comes between them. That was probably not the intention of the exercise.
Hope this helps
- Dan
This goes back some ways but I recently ran into the same issue and once again referred to
http://minnow.cc.gatech.edu/squeak/uploads/SqueakLanguageRef.3.html
on how to create an array literal. When it didn't work again I remembered this old email exchange I had on the subject and it occurred to me that the current language reference does not refer to this (apparently) better array literal formation method.
This is also being CCed to Andrew P. Black, but I'm not sure if he is still the maintainer for this document.
Ken
On Sat, 2002-01-12 at 19:06, Dan Ingalls wrote:
Ken Causey ken@ineffable.com wrote...
OK, I'm beginning to think that I'm missing the obvious. As mentioned in a previous email I'm working through Guzdial's _Squeak OODMA_ and to be more specific I'm working on Exercise 8 of Chapter 2 on page 47. I'm asked to implement a method switchOn that can be used like
#( ('a' [Transcript show: 'An a was input']) ('b' [Transcript show: 'A b was input']) ) switchOn 'a'.
I believe that the Array literal above should result in an Array of 2 elements, each element being an Array of 2 elements (a String and a BlockContext). But... What I end up with is an Array of 2 elements, each element being an Array of 6 (yes six) elements. To be most specific when I inspect element 1 of the outer Array I get an array composed of
1: 'a' 2: #[ 3: #Transcript 4: #show: 5: 'An a was input' 6: #]
Is this correct behaviour?
Yes. Blocks have never been recognized within literal arrays in Squeak.
If you want something that looks like this, and that works, use curly braces instead of parentheses.
{ {'a'. [Transcript show: 'An a was input']}. {'b'. [Transcript show: 'A b was input']} } switchOn 'a'.
The {} construct expects a series of expressions (which can be blocks or anything) separated by '.'.
If you really want to execute the three lines as you gave them, then you'll have to write a recognizer for brackets, and separately ask the compiler to evaluate what comes between them. That was probably not the intention of the exercise.
Hope this helps
- Dan
On Thursday 22 August 2002 01:59 pm, Ken Causey wrote:
the current language reference does not refer to this (apparently) better array literal formation method.
It has a section on 'Brace Arrays'; is that what you're referring to?
On Thu, 2002-08-22 at 16:14, Ned Konz wrote:
On Thursday 22 August 2002 01:59 pm, Ken Causey wrote:
the current language reference does not refer to this (apparently) better array literal formation method.
It has a section on 'Brace Arrays'; is that what you're referring to?
Ah exactly! Shame on me for not finding it. But! This should be up in the "Literals (Constant Expressions)" section immediately after "Constant Arrays (Instances of class Array)". At least it should be in the Literals sections somewhere since that's what it is, right? Why in the world it's between "Case Structures (Receiver is any Object)" and "Answer Expressions" is a bit beyond me.
Ken
-- Ned Konz http://bike-nomad.com GPG key ID: BEEA7EFE
squeak-dev@lists.squeakfoundation.org