Chapter 4: a return settled by an if, built

Chapter 4's describe turns a Boolean into the word known or unknown. Its return starts as an empty String and the body settles it - so the body is an if with an else. The IDE writes the signature and the if … else head; you type the return and the words. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-describe.

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 functions.describe

  defines function

    //Start the return with a simple empty value, then settle it in the body.
    describe()
      -> known as Boolean
      <- result <- String()
      if known
        result: "known"
      else
        result: "unknown"

  defines program

    Show()
      stdout <- Stdout()
      stdout.println(describe(true))
      stdout.println(describe(false))

//EOF

Step 1: A function that returns a String

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, known of type Boolean, and set Returns to String. Choose Preview.

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

The preview shows describe about to be written, known in and String out. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows describe about to be written, known in and String out

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

The EK9 IDE once this is done: Select the describe() 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 the return - an empty String to start - and the first assignment. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type the return - an empty String to start - and the first assignment

Step 2: Settle the return: Surround With ▸ if … else

Put the caret on the result: "known" line, right-click - or choose Refactor > Refactor This… - and choose Surround With ▸ if ... else.

The EK9 IDE once this is done: Put the caret on the result

Select the stub, from surroundGuard <- Boolean(true) down to its //AI line, and type the real test: if known. Saves when typed.

The EK9 IDE once this is done: Select the stub, from surroundGuard <- Boolean(true) down to its //AI line, and type the real test

Select the else and its placeholder, and type the else branch: result settled as unknown. Saves when typed.

The EK9 IDE once this is done: Select the else and its placeholder, and type the else branch

Step 3: The program

Choose Code > New Construct > Program…

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

Show is the program's name. Type Show 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

Select the Show() 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 Show() 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 Show's body: a Stdout and describe called with true and with false. Saves when typed.

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

Step 4: 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.

known, then unknown - describe(true) takes the if, describe(false) the else. Choose Close.

The EK9 IDE just before choosing Close: known, then unknown - describe(true) takes the if, describe(false) the else

The result

Describe.ek9 now reads exactly as chapter 4 of Getting Started with EK9 shows it: a return that starts as an empty String and is given its real value by whichever branch runs. The signature was the tool's, Surround With ▸ if … else wrote the branch head and its else; you typed the return's name, the condition and the words. AI assistants use ek9_new_construct and ek9_surround_with.

The EK9 IDE when the lesson has finished

Where this lesson comes from