Chapter 25: one object, two names, built

Chapter 25 starts with a fact Java and C# readers expect to be the other way round: in EK9 a variable assigned from another is a second NAME for the same object, and an operator such as ++ or += changes that object in place. This program shows it with an Integer and a List, and shows Integer(count) making a real copy. The IDE writes the program's shell; you type the body. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-aliasing.

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

  defines program

    Aliasing()
      stdout <- Stdout()

      count <- 10
      sameObject <- count
      count++
      stdout.println(`count ${count} sameObject ${sameObject}`)

      independent <- Integer(count)
      count++
      stdout.println(`count ${count} independent ${independent}`)

      names <- ["Ann"]
      sameList <- names
      names += "Bo"
      stdout.println(`names ${names} sameList ${sameList}`)

Step 1: The program

Choose Code > New Construct > Program…

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

Aliasing is the program's name. Choose Preview.

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

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

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

Select the Aliasing() 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 Aliasing() 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 Aliasing'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 Aliasing's body exactly as the chapter shows it

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

count 11 sameObject 11, then count 12 independent 11, then the List with Ann and Bo through both of its names. Choose Close.

The EK9 IDE just before choosing Close: count 11 sameObject 11, then count 12 independent 11, then the List with Ann and Bo through both of its names

The result

Aliasing.ek9 now reads exactly as chapter 25 of EK9 in Depth shows it. count++ moved sameObject too, because they were one object; Integer(count) made an independent copy that the next count++ left alone; and += grew both names of the List.

The EK9 IDE when the lesson has finished

Where this lesson comes from