Chapter 31: the four comment forms, built

EK9 has four comment forms and no others. // runs to the end of the line and is the only one Volume 1 used; <!-- --> and <!- -!> are block forms; <?- -?> is the documentation form, for a concept or a reason - the things a compiler cannot check and a comment may state. C's /* */ is not one of them, deliberately. The IDE writes the function's signature and the program's shell; you type the comments. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-comments.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
<?-
  The documentation form, written before the construct it describes.
-?>
defines module book.comments

  defines function

    <?-
      Why this function exists. The compiler checks the lines below; nothing checks this.
    -?>
    describe() as pure
      -> count as Integer
      <- rtn as String: `${count} item(s)` // a line comment runs to the end of the line

  <!--
    A block comment: everything in here is ignored.
  -->

  <!-
    The second block form, also ignored.
  -!>

  defines program

    Comments()
      stdout <- Stdout()
      stdout.println(describe(3))

Step 1: describe — a documented function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

describe is the function's name. Add one parameter, count of type Integer, set Returns to String and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: describe is the function's name

The preview shows describe about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows describe about to be written

Select the describe() as pure line and, above it, type the comment the book puts there. Saves when typed.

The EK9 IDE once this is done: Select the describe() as pure line and, above it, type the comment the book puts there

Select the stub, from <- rtn as String? down to its //AI line, and type describe's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type describe's own return and work, as the book writes them

At the very top of the file, below #!ek9, type the documentation form: <?- -?> marks text as documentation for the construct that follows it - here the module. Saves when typed.

The EK9 IDE once this is done: At the very top of the file, below #!ek9, type the documentation form

Step 2: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

Comments is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: Comments is the program's name

The preview shows Comments about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Comments about to be written

Select the Comments() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the Comments() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type Comments's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type Comments's body exactly as the chapter shows it

Below the function, type the two block forms: <!-- --> for people who think in HTML, and <!- -!>, the one EK9's own tooling uses to comment lines out. To the compiler all four forms are hidden text. Saves when typed.

The EK9 IDE once this is done: Below the function, type the two block forms

Step 3: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

3 item(s). Choose Close.

The EK9 IDE just before choosing Close: 3 item(s)

The result

Comments.ek9 now reads exactly as chapter 31 of EK9 in Depth shows it - all four forms in one file. The documentation comments sit before the module and the function they describe; the rest the compiler never sees.

The EK9 IDE when the lesson has finished

Where this lesson comes from