5

In the book Smalltalk Best Practice Patterns from Kent Beck, the double greater sign (>>) is used to define methods like this:

Point class>>x: xNumber y: yNumber
    ^self new
        setX: xNumber
        y: yNumber

Point>>setX: xNumber y: yNumber
    x := xNumber.
    y := yNumber.
    ^self

However, I cannot get it run in GNU Smalltalk.

Is it valid syntax in some implementation of Smalltalk? Or is it just kind of pseudo code?

Domon
  • 6,753
  • 1
  • 25
  • 29

3 Answers3

5

In fact this is Pseudo code.

In other languages you would use the . to tell people that the method is in this class but in smalltalk you write >>

What you would do in a Smalltalk like Squeak or Pharo for

Point class>>x: xNumber y: yNumber
    ^self new
        setX: xNumber
        y: yNumber
  1. Open the System Browser
  2. klick on class, a button that will show you the class side of the class.
  3. paste the method in the text area with the source code:

    x: xNumber y: yNumber
        ^self new
            setX: xNumber
            y: yNumber
    
  4. Strg-s to save the code

For

Point>>setX: xNumber y: yNumber
    x := xNumber.
    y := yNumber.
    ^self

You would do the same but not use the class side

Seki
  • 11,135
  • 7
  • 46
  • 70
User
  • 14,131
  • 2
  • 40
  • 59
4

Also, notice that indeed, #>> is a message that you can send to a class and it basically access the method dictionary for the symbol (selector argument). See, Behavior class, method >>

  >> selector 
"Answer the compiled method associated with the argument, selector (a 
Symbol), a message selector in the receiver's method dictionary. If the 
selector is not in the dictionary, create an error notification."

^self compiledMethodAt: selector

So you can do, for example (inspect that)

  Point class >> #x:y:

Notice however, that here we send #class because #x:y: is a class side method. If you want to access an instance side method, say #normalized then you can do:

  Point >> #normalized
2

The correct syntax for GNU Smalltalk will look something like this:

Point class extend [
    x: xNumber y: yNumber [
        ^self new
            setX: xNumber
            y: yNumber ]
]

Point extend [
    setX: xNumber y: yNumber [
        x := xNumber.
        y := yNumber.
        ^self ]
]

For more information on GNU Smalltalk syntax see here.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94