E07240: non web service methods cannot be marked with the 'protected' access modifier

Category: E07xxx Full resolution · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: METHOD_MODIFIER_PROTECTED_IN_SERVICE · All error codes

What went wrong

A non-web method in a SERVICE is marked 'protected'. Services don't support protected visibility for internal methods.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1316 (Why can't a helper method in an EK9 service be marked...)

Rationale

Services are entry points - the protected visibility concept (subclass access) doesn't apply to service architectures.

How to fix it

  1. Use 'private' for internal methods
  2. Or leave public (default)

The [EXAMPLE] section below shows a 'protected' helper in a service and the same helper made 'private'.

Common causes

  • Habit from class design
  • Trying to hide helper methods

See also

  • E07240: protected in service (this error)
  • E07250: protected in component

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07240: non web service methods cannot be marked with the 'protected' access modifier

#!ek9
defines module error.e07240.bad

  defines service

    Site for :/site open

      index() as GET for :/
        <- response as HTTPResponse?
        body <- pageBody()
        response: (body) with trait HTTPResponse
          override content()
            <- rtn as Bytes: Bytes().withUtf8(body)
          override status() as pure
            <- rtn as Integer: 200
          override contentType() as pure
            <- rtn as String: "text/plain"
          override cacheControl() as pure
            <- rtn as String: "no-cache"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

      //A service is never extended, so 'protected' means nothing here
      protected pageBody()
        <- rtn as String: "welcome"

Fixed: Make the helper private

#!ek9
defines module error.e07240.good

  defines service

    Site for :/site open

      index() as GET for :/
        <- response as HTTPResponse?
        body <- pageBody()
        response: (body) with trait HTTPResponse
          override content()
            <- rtn as Bytes: Bytes().withUtf8(body)
          override status() as pure
            <- rtn as Integer: 200
          override contentType() as pure
            <- rtn as String: "text/plain"
          override cacheControl() as pure
            <- rtn as String: "no-cache"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

      //A helper only the service calls is private
      private pageBody()
        <- rtn as String: "welcome"

Questions and answers

These Q&A entries show E07240 and its fix: