Search code examples
smalltalkpharo

Creating multiple variables in Pharo gives me a "variable or expression expected" error


I'm confused about why this code seems to work fine in isolation, but I get an error when I put it all together.

The following snippet prints 'Hello World!' when printed:

| blah |
blah := 'Hello '.
blah, 'World!'.

But the following code block gives me the error Variable or expression expected

| blah |
blah := 'Hello '.
blah, 'World!'.
| blah2 |
blah2 := 'World!'.
blah, blah2.

Could someone explain what's going on here?


Solution

  • variable declarations are only allowed at the beginning of a block or method:

    | blah blah2 |
    blah := 'Hello '.
    blah, 'World!'.
    
    blah2 := 'World!'.
    blah, blah2.