Chapter 37: a status decided late, built
When is an HTTP status decided? Usually before the body - but a body can discover a problem. Chapter 37's LateStatus builds a response class whose status is read after its content is made, and a route that returns it. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-late-status.
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.notesservice.latestatus defines constant LATE_PORT <- 8084 STATUS_OK <- 200 STATUS_NOT_FOUND <- 404 defines class LateResponse with trait of HTTPResponse code as Integer: STATUS_OK override status() as pure <- rtn as Integer: code override content() <- rtn as Bytes: Bytes() code := STATUS_NOT_FOUND rtn: Bytes().withUtf8("there is no such note") override contentType() as pure <- rtn as String: "text/plain" default operator ? defines service Late for :/late open check() as GET for :/ <- response as HTTPResponse: LateResponse() defines application LateApp register Late() defines program ServeLate() with application of LateApp try -> server <- HTTP(NetworkProperties(port: LATE_PORT, localOnly: true)) server.serve() //EOF
Step 1: Constants
Choose Code > New Construct > Constant…
LATE_PORT is the constant's name and 8084 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.
The preview shows LATE_PORT about to be written. Choose Apply.
Choose Code > New Construct > Constant…
STATUS_OK is the constant's name and 200 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.
The preview shows STATUS_OK about to be written. Choose Apply.
Choose Code > New Construct > Constant…
STATUS_NOT_FOUND is the constant's name and 404 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.
The preview shows STATUS_NOT_FOUND about to be written. Choose Apply.
Step 2: LateResponse — a class
Choose Code > New Construct > Class…
LateResponse is the class's name. It is a response that can change its mind: its status is read after its content is made. Choose Preview.
The preview shows LateResponse about to be written. Choose Apply.
Select the LateResponse line and, above it, type the blank line the book opens the block with. Saves when typed.
Right-click the LateResponse line and choose Add Field…
Type code as Integer: STATUS_OK. The status it will report, until changed. Choose OK.
The preview shows code added - and default operator ? with it, LateResponse's first field. Choose Apply.
Right-click the LateResponse line and choose Adopt Trait…
Type HTTPResponse as the trait - Implement method stubs is already chosen - and choose OK.
Adopt Trait changes the open file in the editor, not yet saved. Choose File > Save.
Right-click the LateResponse line and choose Add Method…
status is the method's name. It is HTTPResponse's own method. Set Return type to Integer, tick pure and override. Choose Apply.
Select the stub and type the answer: the code. Saves when typed.
Right-click the LateResponse line and choose Add Method…
content is the method's name. It is the body. Set Return type to Bytes and tick override. Choose Apply.
Select the stub and type the body - and, while making it, the late decision: the status becomes not found. Saves when typed.
Right-click the LateResponse line and choose Add Method…
contentType is the method's name. Set Return type to String, tick pure and override. Choose Apply.
Select the stub and type the answer. Saves when typed.
Step 3: Late — a service
Choose Code > New Construct > Service…
Late is the service's name and /late the base URI every route sits under. One route, with no path variable. The Routes table holds check, GET /. Choose Preview.
The preview shows Late about to be written, with its route, answering with an HTTPResponse the tool wrote. Choose Apply.
Select the Late line and, above it, type the blank line the book opens the block with. Saves when typed.
The tool wrote a response of its own; the book returns the LateResponse. Select it, from its first line down to the default operator, and type the one line. Saves when typed.
Step 4: LateApp — an application
Choose Code > New Construct > Application…
LateApp is the application's name. It registers the service. The Registrations table holds Late. Choose Preview.
The preview shows LateApp about to be written, with its register line. Choose Apply.
Select the LateApp line and, above it, type the blank line the book opens the block with. Saves when typed.
Step 5: ServeLate — a program
Choose Code > New Construct > Program…
ServeLate is the program's name. Leave Application empty - a later step says why. Choose Preview.
The preview shows ServeLate about to be written. Choose Apply.
Select the ServeLate line and, above it, type the blank line the book opens the block with. Saves when typed.
The tool cannot write a program bound to an application that registers a service - the program must create the HTTP server, which no stub can - so it wrote it unbound. Select the stub, from the program's header down to its //AI line, and type the header with its application and the body that serves, on this machine only. Saves when typed.
The result
LateStatus.ek9 now reads exactly as chapter 37 of EK9 in Depth shows it. The response answers 200 until its content is made, and the content makes it 404 - the status is read afterwards. Built, not run: the program serves until sent TERM.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_adopt_trait,ek9_add_field,ek9_add_method,ek9_add_application,ek9_add_program.










































