The intention of this page is to supply a 'quick look' at SmallScript syntax for those beginning in the language. I'd appreciate any additions or corrections that you may have. Please email Chris Double if you have any comments.

The content of this page was originally modelled after the examples at 'Goo By Example'. The intention is not to show SmallScript as a 'better' language but rather I thought the Goo examples were great and wanted to have a language to compare against.

History

1.0 - Initial version.

Simple Function

  Function[
  double(x)
 	  ^2 * x.
  ]

  stdout << 'double: ' << double(10).

Smalltalk Syntax Simple Function

  Function[
  doubleNumber: x
    ^2 * x.
  ]

  stdout << 'doubleNumber: ' << (doubleNumber: 30).

Output a message

  Function [
  helloWorld
    stdout << 'Hello World'; cr.
  ]

Output a slightly more complicated message

  Function [
  helloNumber(n)
    stdout << 'Hello Number ' << n << '!'; cr.
  ]

Attach types to arguments

  Function [
  helloInteger(<Integer> n)
    stdout << 'Hello Integer ' << n << '!'; cr.
  ]

Attach a type to your return (type)

  Function [ <Integer>
  doubleInteger(<Integer> n)
    ^2 * n.
  ]

Multimethod Dispatch

  Function [ 
  hello(<Integer> x)
    stdout << 'Hello integer ' << x; cr.
  ]

  Function [ 
  hello(<Float> x)
    stdout << 'Hello floating point ' << x; cr.
  ]

Fun with string concatenation

  Function [
  didYou(<String> thing)
    ^'No! I didn''t ' + thing.
  ]

Defining variables

  Function [
  formalHello(<String> name)
    |formalName| := 'mister ' + name.
    stdout << 'Hello ' << formalName; cr
  ]

Defining closures (blocks)

  Function [
  quadruple(>Integer> x)
    |double| := [ :y | 2 * y ].
    ^double(double(x)).
  ]

Collection processing for fun and profit

  Function [
  makeNamesStupid(<Collection> names)
    ^names do: [ :theName | 'Smelly ' + theName ]
  ]

  Function [
  insultManyPeople
    |people| := #('Santa', 'Easter Bunny', 'Ringo').
    makeNamesStupid(people) 
     do: [ :name | stdout << name << ' is Smelly!'; cr ]
  ]

Classes

  class name: Animal
        fields: auto bathroom
  {
    Function [
    new
      ^super new initialize; yourself.
    ]
  }

  class name: Cat
        extends: Animal
  {

    Method [
    initialize
      bathroom := 'dirt'.
    ].
  }

  class name: Dog
        extends: Animal
  {
    Method [
    initialize
      bathroom := 'tree'.
    ].
  }

  Function [
  testCatDog
    |myCat| := Cat new.
    |myDog| := Dog new.
    stdout << 'Cat: ' << myCat bathroom; cr.
    stdout << 'Dog: ' << myDog bathroom; cr.
  ]

Looping

  Function [
  testWhile
    |a| := 0.
    while(a < 10)
    [
      a :+= 1.
      stdout << a; cr.
    ]
  ].

  Function [
  usingToAndDo
    1 to: 10 do: [ :a | stdout << a; cr ]
  ].

Conditionals

  Function [
  conditional1(a, b)
    ^if(a < b) then [a] else [b]
  ]

  Function [
  conditional2(a, b)
    ^a < b ifTrue: [a] ifFalse: [b].
  ]

  Function [
  conditional3(a, b)
    ^(a < b) ?: a : b.
  ]

  Function [
  conditional4(failed)
    ^unless(failed) [ 'succeeded' ].
  ]

  Function [
  conditional5(a, b, c)
    if(b >= a and: [b <= c])
       [stdout << 'b is between c and a']
     else
       [stdout << 'b is not between c and a']
  ]

  Function [
  conditional6(a)
    ^if(a = 10) 
       ['ten']
     else if (a = 'ten')
       [10]
     else if (a = 'eleven')
       [11]
     else if (a = 11)
       ['eleven']
     else
       [a]
   ]

  Function [
  conditional7(a)
    ^switch(a)
     [
       case 10: 
         'ten'. 
         break.
       case 'ten': 
         10. 
         break.
       case 'eleven': 
         11.
         break.
       case 11:
         'eleven'.
         break.
       caseIf (a respondsTo(#isOdd) and: [a isOdd]):
         'a is odd'.
         break.
       default:
         a.
     ].
   ]