www.delorie.com/gnu/docs/smalltalk/gst_47.html   search  
 
Buy GNU books!


GNU Smalltalk User's Guide

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.5 Math in Smalltalk

In this case, what happened was that the object 9 (an Integer), received a + message with an argument of 7 (also an Integer). The + message for integers then caused Smalltalk to create a new object 16 and return it as the resultant object. This 16 object was then given the printNl message, and printed 16 on the terminal.

Thus, math is not a special case in Smalltalk; it is done, exactly like everything else, by creating objects, and sending them messages. This may seem odd to the Smalltalk novice, but this regularity turns out to be quite a boon: once you've mastered just a few paradigms, all of the language "falls into place". Before you go on to the next chapter, make sure you try math involving * (multiplication), - (subtraction), and / (division) also. These examples should get you started:

 
   (8 * (4 / 2)) printNl !
   (8 - (4 + 1)) printNl !
   (5 + 4) printNl !
   (2/3 + 7) printNl !
   (2 + 3 * 4) printNl !
   (2 + (3 * 4)) printNl !


  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003