E05070: inappropriate use of 'this'

Category: E05xxx Explicit type symbol definition · Reported in phase: FULL_RESOLUTION · Classification: INAPPROPRIATE_USE_OF_THIS · All error codes

What went wrong

You're using 'this' in a form the surrounding construct cannot support. 'this' itself is not the problem: in a class, record or component it is the current object, and in a function it is the function itself (a function is an object, so 'fn <- this' compiles). What is rejected is calling 'this' as a constructor where there is no constructor to run:

  • calling 'this(...)' from a function, which has no constructors to delegate to (in a method or a program the same call is E05060)
  • 'register this()' in an application, which has no 'this' to register

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 787 (What does 'this' mean in a function in EK9, and what tri...)

[CONTEXTS WHERE 'THIS(...)' IS VALID]

  • The first statement of a constructor, delegating to another constructor of the same type

[CONTEXTS THAT RAISE E05070]

  • 'this()' or 'this(args)' inside a function
  • 'register this()' inside an application

How to fix it

  1. In a function: call the function or method you meant by name, or pass the value in as a parameter - there is no constructor to delegate to
  2. In an application: register a real component, e.g. 'register Logger()'
  3. If you need instance state: make the code a method on a class and use 'this.field' there

Common causes

  • Copy-pasting constructor code (with its 'this(...)' delegation) into a standalone function
  • Confusing 'defines function' with a method inside a class
  • Writing 'register this()' as if an application were a component

See also

  • E05060: 'this()' or 'super()' outside a constructor in a method or program
  • E05080: 'super' without explicit parent class
  • E05090: direct assignment to 'this' (use :=:, :~: or a mutating operator)

Examples

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

Fails with E05070: inappropriate use of 'this'

#!ek9
defines module error.e05070.bad

  defines function

    describe() as pure
      -> enabled as Boolean
      <- rtn as String: enabled <- "on" else "off"
      //A function is not constructed: this() belongs only in a constructor
      this()

Fixed: Drop the this() call: a function just computes its result

#!ek9
defines module error.e05070.good

  defines function

    describe() as pure
      -> enabled as Boolean
      <- rtn as String: enabled <- "on" else "off"

Questions and answers

These Q&A entries show E05070 and its fix: