The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Smalltalk Syntax...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Smalltalk Syntax... Posted: Jul 30, 2004 7:39 PM
Reply to this message Reply

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
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Michael Lucas-Smith

Advertisement

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:

myObject eatDonuts: (myObject donutEatingTolerence) forDays: 10
myObject.eatDonutsForDays(myObject.donutEatingTolerence(), 10)

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:

myObject eatDonuts: myObject donutEatingTolerence forDays: 10

If we wanted to call another method with parameters, we'd have to use brackets:

myObject eatDonuts: (myObject donutEatingTolerence: 'glazed') forDays: 10

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.

Read: Smalltalk Syntax...

Topic: Everything old is new again Previous Topic   Next Topic Topic: if(extremeProgramming.equals(scientificMethod))

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use