Chapter 33: the answer a key would lose, built

Inside a key you carry a result out by changing a captured variable, and the verb that changes it is :=:, not :. A capture shares the caller's object, so reassigning it with : would point only the key's own name at another object, and the caller would never see the answer. This lesson types it that way on purpose and lets the compiler refuse it, then puts it right. The IDE writes the constant, the class with its lock, hasFunds' shape and the program's shell; you type the key, its fix and the call. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-lost-result.

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.concurrency.lostresult

  defines constant
    withdrawal <- 80

  defines class

    Account
      lockedBalance <- MutexLock(100)

      hasFunds()
        -> amount as Integer
        <- rtn as Boolean: false
        checker <- (amount, rtn) is MutexKey of Integer as function
          rtn :=: lockedItem >= amount
        require lockedBalance.enter(checker)

      default operator ?

  defines program

    LostResult()
      stdout <- Stdout()
      account <- Account()
      stdout.println(`funds for ${withdrawal}: ${account.hasFunds(withdrawal)}`)

Step 1: A constant

Choose Code > New Construct > Constant…

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

withdrawal is the constant's name and 80 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: withdrawal is the constant's name and 80 its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: Account — a balance under a lock

Choose Code > New Construct > Class…

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

Account is the class's name. Choose Preview.

The EK9 IDE just before choosing Preview: Account is the class's name

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

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

Select the Account line and, above it, type the blank line the book opens its first class with. Saves when typed.

The EK9 IDE once this is done: Select the Account line and, above it, type the blank line the book opens its first class with

Right-click the Account line and choose Add Field…

The EK9 IDE once this is done: Right-click the Account line and choose Add Field…

Type lockedBalance <- MutexLock(100) - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type lockedBalance <- MutexLock(100) - the field and the value it starts with

The preview shows lockedBalance added - and default operator ? with it, Account's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows lockedBalance added - and default operator ? with it, Account's first field

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

hasFunds is the method's name. Add one parameter, amount of type Integer, set Return type to Boolean. Choose Apply.

The EK9 IDE just before choosing Apply: hasFunds is the method's name

Select the stub and type hasFunds' key the WRONG way, on purpose: rtn: would point the key's own name for rtn at another object, and the caller would never see it. Saves when typed; the compile then reports E08280, a dynamic function re-pointing a variable it captured.

The EK9 IDE once this is done: Select the stub and type hasFunds' key the WRONG way, on purpose

Select the re-point and type the fix: rtn :=: copies the answer into the Boolean the caller holds, the one object both names share. Saves when typed, and the compile is clean.

The EK9 IDE once this is done: Select the re-point and type the fix

Step 3: The program

Choose Code > New Construct > Program…

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

LostResult is the program's name. Choose Preview.

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

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

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

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

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.

funds for 80: true - the answer reached the caller. Choose Close.

The EK9 IDE just before choosing Close: funds for 80

The result

LostResult.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it: the compiler refused the re-point with E08280, and rtn :=: lockedItem >= amount changed the one object both names share, so the run shows that a balance of 100 covers the request for 80.

The EK9 IDE when the lesson has finished

Where this lesson comes from