Chapter 7: constants, built

Chapter 7's Constants declares four values at module level - an Integer, a String, a Money and a Time - for the whole program to share. New Construct ▸ Constant… writes each one from its literal; the program that prints them is typed. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-constants.

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.constants

  defines constant
    maxRetries <- 3

    greeting <- "Hello"

    ticketPrice <- 12#GBP

    openingTime <- 09:00

  defines program
    Show()
      stdout <- Stdout()
      stdout.println(`retries allowed: ${maxRetries}`)
      stdout.println(`${greeting} — doors open at ${openingTime}, entry ${ticketPrice}`)

Step 1: Constants

Choose Code > New Construct > Constant…

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

maxRetries is the constant's name and 3 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: maxRetries is the constant's name and 3 its value - a literal in the Value field, from which the type is inferred

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

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

Choose Code > New Construct > Constant…

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

greeting is the constant's name and "Hello" its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: greeting is the constant's name and "Hello" its value - a literal in the Value field, from which the type is inferred

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

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

Choose Code > New Construct > Constant…

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

ticketPrice is the constant's name and 12#GBP its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: ticketPrice is the constant's name and 12#GBP its value - a literal in the Value field, from which the type is inferred

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

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

Choose Code > New Construct > Constant…

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

openingTime is the constant's name and 09:00 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: openingTime is the constant's name and 09:00 its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: 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. 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 stub, from ready <- true down to its //AI line, and type Show'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 Show's 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.

retries allowed: 3, then Hello — doors open at 09:00:00, entry 12.00#GBP. Choose Close.

The EK9 IDE just before choosing Close: retries allowed

The result

Constants.ek9 now reads exactly as chapter 7 of Getting Started with EK9 shows it: four constants whose types are inferred from their literals, used by a program in the same module.

The EK9 IDE when the lesson has finished

Where this lesson comes from