Numeric types and Conditionals TopAnnouncementsIndenting

Indenting

[This section has more detail than usual since there is not a good description of the indenting rules in the text.]

Indenting is important in Grace. All statements at the same level should be indented exactly the same amount or the compiler will get confused.

Suppose I have a command that is too long to fit on one line without going off the end of my window. If so, I can continue it on the next line as long as the continuation line is indented from the first line. Thus

def x: Number = 7 +
  3

is fine (but not very readable), and results in x standing for 10. However the following is not fine:

def x: Number = 7 +
3

The problem with this second version is that it looks like the second line is the beginning of a new statement. Rather than trying to decide in an ambiguous context, the Grace compiler simply assumes that if a line is indented more than the previous line, then it must be a continuation of that line. Thus if you write

print "hello"
 print "there"

you will get the following error message:

Syntax error: Unknown method 'print()print'. This may be a spelling mistake or an
  attempt to access a method in another scope
    in "testindent" (line 1, column 1)

This error message is correct in that the system assumes that the second line is a continuation of the first and the whole thing must be a message send equivalent to

print "hello" print "there"

So, because there is no method print()print, the error message is correct, but not very helpful. When you see an error message like this, check to see if the indentation is correct on the following line!

But wait a minute, I do change indentation in my Grace programs. For example, consider the following program:

def simpleDrag: GraphicApplication = object {
   inherits graphicApplication.size (400, 400)

   // create the block to be dragged
   def block: Graphic2D = framedRect.at (100 @ 120) size (60,60) on (canvas)
   
   // Drag the upper-left corner of the block to where the mouse is
   method onMouseDrag(point: Point) -> Done {
     block.moveTo (point)
   }
   
   startGraphics
}

The inherits line is more indented than the line above it, and the body of the onMouseDrag method is indented more than the method header.

The reason for this has to do with blocks in Grace. Blocks are sections of code that begin and end with curly braces "{" and "}". Blocks are sections of code that belong together because they are part of a single definition. For example, the first line above defines an object named simpleDrag. The first line ends with a "{". Everything between that open brace and the matching "}", which occurs as the last line are to be grouped together and they all must be indented from that first line. Thus you'll notice that the lines starting with inherits, def, method, and startGraphics are all lined up and indented 3 spaces from the first line. Notice as well that the closed curly brace on the last line is indented the same amount as the line with the starting "{".

Now the body of the onMouseDrag method is also indented from the method header (the line that starts with method). That is because the line with the method header also ends with a "{". Its body is a block as well. Thus the line block.moveTo (point) is indented another 3 spaces. The closing "}" is lined up under the method header.

Grace will let you indent by as much or as little as you like, as long as you are consistent within a block. However I recommend that you always indent at least two spaces. In the lower left corner of the minigrace web page, there is a button that allows you to adjust the settings of the editing window. There you will see that you can set the tab size to be either 2 or 4. That can be helpful as you can now use the tab key instead of counting spaces. I would also urge you to use that to select "Show Indent Guides" as that will generate (very faint) vertical lines that helps you line up your code properly.


Numeric types and Conditionals TopAnnouncementsIndenting