Why must all EK9 service methods declare a return value?

← Web Services · Ref: Q869

ALL EK9 service methods must have a return declaration. Services are HTTP endpoints and must return an HTTPResponse to express status codes, headers, and content. A method without a return declaration triggers E07800.

CORRECT PATTERN

  healthCheck() as GET for :/status
    <- response as HTTPResponse: () with trait HTTPResponse
      ...

The method declares a return of HTTPResponse.

INCORRECT PATTERN

  healthCheck() as GET for :/status
    require true

No return declaration — the compiler cannot determine what HTTP response to send.

See Q845 for service return type. See Q659 for service return patterns.

Example

defines module qa.webdeep.service.needs.return

  defines service

    <?-
      Service with proper return declaration.
      All service methods must return HTTPResponse.
    -?>
    StatusService :/api open

      healthCheck() as GET for :/status
        <- response as HTTPResponse: () with trait HTTPResponse
          override content()
            <- rtn as String: `{"status": "healthy"}`
          override status() as pure
            <- rtn as Integer: 200
          override contentType() as pure
            <- rtn as String: "application/json"
          override cacheControl() as pure
            <- rtn as String: "no-cache"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

  defines application

    StatusApp
      register StatusService()

  defines program

    ServiceReturnDemo()
      stdout <- Stdout()
      stdout.println("All service methods must declare a return value")
      stdout.println("The return type must be compatible with HTTPResponse")

Common mistakes

E07800 — Service methods must always return an HTTPResponse. Omitting the return declaration means the compiler cannot determine the HTTP response. See ek9 -h E07800 for details.

Incorrect:

      healthCheck() as GET for :/status
        require true

Correct:

      healthCheck() as GET for :/status
        <- response as HTTPResponse: () with trait HTTPResponse
          override content()
            <- rtn as String: `{"status": "healthy"}`
          override status() as pure
            <- rtn as Integer: 200
          override contentType() as pure
            <- rtn as String: "application/json"
          override cacheControl() as pure
            <- rtn as String: "no-cache"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?
Other ways to ask this
  • What triggers E07800 SERVICE_MISSING_RETURN?
  • Why can't my service method omit the return declaration?
  • What return type do EK9 service methods require?

Coming from another language?

Java Spring: void controllers write to response directly. C# ASP.NET: void actions with Response.Write. Go: handlers write to ResponseWriter without return. Python Flask: can return None (500 error). EK9: mandatory HTTPResponse return ensures every endpoint has a well-defined response.

Keywords: missing, endpoint, E07800, return, HTTPResponse, service, method