Chapter 32: a holder that closes what it holds, built

Java closes a try-with-resources before the catch and finally run; C# has using, Python has with. EK9 will not let you write the close at all: a closeable is a type with an operator close, and a try that is handed one owns it. A class that keeps closeables in its properties says default operator close and the compiler writes the close for it, last property first. The IDE writes Connection with its field and constructor and the shape of its close, and Session with its two closeable fields; you type what the constructor and close print and the try. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-session-close.

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.resources.session

  defines class

    Connection
      name <- String()

      Connection()
        -> label as String
        name := label
        Stdout().println(`opened ${name}`)

      operator close as pure
        Stdout().println(`closed ${name}`)

      default operator ?

    Session
      primary <- Connection("primary")
      replica <- Connection("replica")

      send()
        -> message as String
        Stdout().println(`sending ${message}`)

      default operator ?
      default operator close

  defines program

    SessionClose()
      stdout <- Stdout()
      try
        -> session <- Session()
        session.send("hello")
        throw Exception("link dropped")
      catch
        -> ex as Exception
        stdout.println(`caught ${ex.reason()}`)
      finally
        stdout.println("finally")

Step 1: Connection — a closeable

Choose Code > New Construct > Class…

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

Connection is the class's name. Choose Preview.

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

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

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

Select the Connection 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 Connection line and, above it, type the blank line the book opens its first class with

Right-click the Connection line and choose Add Field…

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

Type name <- String() - the field and the value it starts with. Choose OK.

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

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

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

Right-click the Connection line and choose Add Constructor > From All Fields - a constructor that takes every field.

The EK9 IDE once this is done: Right-click the Connection line and choose Add Constructor > From All Fields - a constructor that takes every field

Select the constructor and type the book's: a label in, name set from it, and a line that says it opened. Saves when typed.

The EK9 IDE once this is done: Select the constructor and type the book's

Right-click the Connection line and choose Add Operator > Custom Operator…

The EK9 IDE once this is done: Right-click the Connection line and choose Add Operator > Custom Operator…

close is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field. Choose OK.

The EK9 IDE just before choosing OK: close is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field

Select the stub and type the close: it says it closed. A type with an operator close is a closeable - the compiler calls it, you never do. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the close

Step 2: Session — it owns two connections

Choose Code > New Construct > Class…

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

Session is the class's name. Choose Preview.

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

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

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

Type the two connections the session holds, below its line. Add Field cannot write the first of them: a class that holds a closeable must declare its close (E81067), and a close with nothing to close is refused (E81070) - so the fields come first, and the close right after. Saves when typed; the compile then reports E07235, a class with fields and no ? yet, which the next two steps put right.

The EK9 IDE once this is done: Type the two connections the session holds, below its line

Right-click the Session line and choose Add Operator > close (closeable) > default - the compiler writes the operator from the fields.

The EK9 IDE once this is done: Right-click the Session line and choose Add Operator > close (closeable) > default - the compiler writes the operator from the fields

Right-click the Session line and choose Add Operator > ? (isSet) > default - the compiler writes the operator from the fields.

The EK9 IDE once this is done: Right-click the Session line and choose Add Operator > ? (isSet) > default - the compiler writes the operator from the fields

Right-click the Session line and choose Add Method…

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

send is the method's name. Add one parameter, message of type String. Choose Apply.

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

Select the stub and type send's work. Saves when typed.

The EK9 IDE once this is done: Select the stub and type send's work

Step 3: The program

Choose Code > New Construct > Program…

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

SessionClose is the program's name. Choose Preview.

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

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

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

Select the SessionClose() 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 SessionClose() 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 SessionClose'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 SessionClose'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.

opened primary, opened replica, sending hello, caught link dropped, finally, closed replica, closed primary. Choose Close.

The EK9 IDE just before choosing Close: opened primary, opened replica, sending hello, caught link dropped, finally, closed replica, closed primary

The result

SessionClose.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. The session is the try's resource: it was closed after the catch and the finally, and it closed replica before primary - the close the compiler wrote for default operator close, last property first.

The EK9 IDE when the lesson has finished

Where this lesson comes from