v0.13.7
errors and warnings / types

errors and warnings / types / E2052

E2052 error

Non-optional class field not definitely assigned in the constructor (strict mode): before it returns or calls one of its own methods on self, which may read the field (the message names that call when the field is assigned after it)

area
types (x2xxx)
fails the build
yes
in plain Lua files
reported as W2052

Strict mode: a class field that isn't optional and has no initializer isn't assigned on every path through the constructor. It must be assigned before the constructor returns, and before it calls one of the class's own methods on self, which could read it (the message names that call). Assign it, give it an initializer (level: number = 1), or make it optional (level?: number).

Example

× failswowlua
--!strictclass Player    name: string    level: number    constructor(name: string)        self.name = name    endendprint(Player.new("a"))
✓ compileswowlua
--!strictclass Player    name: string    level: number    constructor(name: string)        self.name = name        self.level = 1    endendprint(Player.new("a"))

The compiler reports it like this:

error[E2052]: field `level` is not assigned in the constructor (initialize it, assign it, or make it optional)
  ┌─ src/main.wowlua:4:5
  │
4 │     level: number
  │     ^^^^^ here
codemessage
E2005Value may be nil (strict mode)

Offline

terminal
$ wowlua explain E2052
E2052 (error): Non-optional class field not definitely assigned in the constructor (strict mode): before it returns or calls one of its own methods on self, which may read the field (the message names that call when the field is assigned after it)

source on GitHub ↗