Guard flattening: the nested version

The complete source of forAI_guard_flattening_before.ek9, compiled by the EK9 compiler. Shown on forAI.html.

#!ek9
defines module site.forAI.guardsbefore

  defines class

    Customer
      credit as Boolean: false

      Customer()
        -> credit as Boolean
        this.credit :=: credit

      hasCredit() as pure
        <- rtn as Boolean: credit

      default operator ?

    Order
      reference as String: String()
      customer as Customer: Customer()

      Order()
        ->
          reference as String
          customer as Customer
        this.reference :=: reference
        this.customer: customer

      id() as pure
        <- rtn as String: reference

      isValid() as pure
        <- rtn as Boolean: reference?

      getCustomer() as pure
        <- rtn as Customer: customer

      default operator ?

  defines function

    processOrder()
      -> order as Order
      <- result as String: String()

      if order?
        if order.isValid()
          if customer <- order.getCustomer()
            if customer.hasCredit()
              result: "Processing " + order.id()

  defines program

    ProcessOne()
      stdout <- Stdout()
      stdout.println(processOrder(Order("A-1", Customer(true))))

Download forAI_guard_flattening_before.ek9

Output

Processing A-1