This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Smalltalk Syntax...
Feed Title: Michael Lucas-Smith
Feed URL: http://www.michaellucassmith.com/site.atom
Feed Description: Smalltalk and my misinterpretations of life
I thought people might find it useful to have a random introduction to Smalltalk syntax. It's not as hard as it seems, but it does take a while to get your head around it. So, lets start with the basics.
First off the bat, there are no exceptions in the Smalltalk syntax and precedence is always from Left to Right.
To explain as I go, I'll give each example in a pseudo-C like Syntax, and also Smalltalk:
myObject helloWorld
myObject.helloWorld()
In this example, I'm sending helloWorld to myObject. On to some more basics:
myObject eatDonuts: 5
myObject.eatDonuts(5)
Here we're passing a parameter to the method eatDonuts:. In Smalltalk, we have what's called keyword selectors, which is a fancy way of saying every parameter is named. This is not named parameters btw, you cannot reorder them. The order of the keywords defines the method you're calling, so:
myObject eatDonuts: 5 forDays: 10
myObject.eatDonutsForDays(5, 10)
In this example, we're giving more meaning to the parameters. The method we're calling in Smalltalk is eatDonuts:forDays:, not eatDonuts: and then forDays:. This is the trickiest bit to get your head around. If we want to call another method for one of our method parameters:
Brackets lets you nest calls, the same what it does in C and other languages. There's a shorthand in Smalltalk for situations like the above. Because there is no argument for donuteatingTolerence, you do not need to put it in brackets, the Smalltalk compiler can infer that it's not part of the eatDonuts:forDays: call. Eg:
You can have up to 255 parameters to a method call in standard Smalltalk-80. I'm not sure if any Smalltalks around let you go beyond this - and in all honesty, it'll be a rare day in hell that you ever need to.
Now that we've talked about syntatical structure a bit, lets introduce the years-old names for things you have in C# and Java.
self. This is 'this'.
super. This is 'super'. If you don't know what super is, it lets you access the superclass version of 'self'.
^. This is 'return'
'I am a string'. This is "I am a string"
"I am a comment". This is /* I am a a comment */
$t. This is 't'
16r20. This is 0x20
true. This is True
false. This is False
. This is something new and I'll come back to it in a later blog post. It's called a BlockClosure. For those familiar with Ruby and Lisp, it's also "Lambda".
For the purposes of clarity, the Transcript is equivalent to stdin/stdout in Smalltalk. So when you see people do this:
Transcript show: 'hello'
stdout << "hello"
.. is what they're doing. Now that we can print out stuff, lets do a bit of type conversion:
Transcript show: 10 asString
stdout << 10.toString
Transcript show: false asString
stdout << False.toString
And so on and so forth. This is the basics of writing Smalltalk code. The real magic comes when you use a Smalltalk IDE. The question that I raised when I was learning smalltalk was "How do I know what I can send to things" .. ie: What are the API's?. In Smalltalk this isn't an issue. You don't need to go out and buy ten books from O'Reilly. You open up a Smalltalk browser, go to the class of interest and all the methods, complete with source, are there for you to peruse.
It gets even better of course, but that's for discussion from other people about the debugger, the inspector and other tools in the system.