What does 'this' mean inside a standalone function, and why can't I reach fields through it?
← Type Hierarchy Constraints · Ref: Q605
Inside a standalone function, 'this' is legal - it refers to the function itself (a function is an object in EK9, so it can be passed around as a value). What a function does NOT have is an instance with fields, so 'this.count' in a function fails: there is no 'count' on the function, and the compiler reports E50001 (not resolved).
WHAT 'this' REFERS TO
- In class and record methods, constructors and operators: the current object, so 'this.count' reaches its fields.
- In a dynamic class body: that dynamic class instance.
- In a standalone function: the function itself. 'me <- this' compiles; 'this.count' does not.
E05070 IS SOMETHING ELSE
E05070 is about CALLING 'this()' where there is no constructor to delegate to - inside a function - or assigning to 'this' where that is not allowed. (Calling 'this()' in a method is E05060.) Merely naming 'this' in a function is not an error.
COMMON MISTAKE
Developers coming from Java or C# sometimes write standalone functions and use 'this' as if they were methods with access to instance state. In EK9, functions only have their parameters, their locals and (for dynamic functions) their captures.
WHAT TO DO INSTEAD
If you need instance state, make it a method on a class. If the function just needs data, pass it as a parameter. If you accidentally wrote 'defines function' instead of a method inside a class, restructure the code.
See Q49 for standalone functions. See Q596 for function vs method distinction. See Q580 for using 'this' correctly in constructor delegation.
Example
defines module qa.typehierarchy.thiscontext defines class Counter count <- 0 Counter() -> initialCount as Integer //this is valid in constructors this.count: initialCount increment() //this is valid in methods this.count: this.count + 1 currentCount() as pure <- rtn as Integer: count operator $ as pure <- rtn as String: `Counter(${count})` default operator ? defines function //Standalone function: 'this' would be the function itself, which has //no fields - so work from parameters and locals doubleValue() as pure -> inputValue as Integer <- result as Integer: inputValue * 2 testThisContext() //Functions work with parameters, not fields reached through 'this' doubled <- doubleValue(21) require doubled == 42 //Classes use 'this' internally counter <- Counter(10) counter.increment() require counter.currentCount() == 11
Common mistakes
E50001 — Inside a standalone function 'this' is the function itself, which has no 'count' field, so this.count cannot be resolved. Pass the value in as a parameter. See ek9 -h E50001 for details.
Incorrect:
doubleValue() as pure -> inputValue as Integer <- result as Integer: this.count * 2
Correct:
doubleValue() as pure -> inputValue as Integer <- result as Integer: inputValue * 2
E50060 — Integer has no intValue() method in EK9. Integer values are used directly without conversion methods. See ek9 -h E50060 for details.
Incorrect:
require counter.currentCount().intValue() == 11
Correct:
require counter.currentCount() == 11
Other ways to ask this
- Can I use this.field inside a function in EK9?
- Where is 'this' valid in EK9?
- Why does using 'this' in a function cause an error?
Coming from another language?
Java: 'this' only in instance methods (static methods cannot use it). Python: 'self' must be explicit parameter, not available in module functions. C#: 'this' only in instance methods. Kotlin: 'this' in member functions only, extension functions use receiver. JavaScript: 'this' binding is complex, arrow functions inherit outer 'this'. EK9: 'this' in a method is the object; in a standalone function it is the function itself, so 'this.field' there is E50001 (not resolved).
Keywords: migrate, hierarchy, class, circular, type, context, scope, instance, inherit, this, function, method, standalone, E50001