Chapter 2: escapes in a string, typed

Chapter 2 ends its strings with escapes: \n for a new line, \t for a tab, \u followed by four hex digits for any character. No tool writes them - they are typed - so the IDE writes the program's shell and you type the body. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-escapes.

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
defines module book.escapes

  defines program
    Show()
      stdout <- Stdout()

      stdout.println("Roses are red,\nViolets are blue")
      stdout.println(`Name:\tAda`)
      stdout.println(`Caf\u00E9 costs \u00A31`)
      stdout.println(`Café costs £1`)

Step 1: The program's shell

Choose Code > New Construct > Program…

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

Show is the program's name. Type it and choose Preview.

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

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

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

Step 2: The body, typed

Select the stub, from ready <- true down to its //AI line, and type the 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 the body exactly as the chapter shows it

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.

Two lines of verse, a tab between Name: and Ada, then Café costs £1 twice - once from escapes, once typed directly. Choose Close.

The EK9 IDE just before choosing Close: Two lines of verse, a tab between Name

The result

Escapes.ek9 now reads exactly as chapter 2 of Getting Started with EK9 shows it: a new line and a tab by escape, and the same accented letter and pound sign written both as \u escapes and as the characters themselves - the last two lines print the same.

The EK9 IDE when the lesson has finished

Where this lesson comes from