v0.13.7
errors and warnings / types

errors and warnings / types / E2005

E2005 error

Value may be nil (strict mode)

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

Strict mode only: the value may be nil, and it is used where nil doesn't fit, such as calling a method on it, indexing it or passing it for a parameter that doesn't take nil. In game that is the "attempt to index a nil value" error. Check the value first (if s then … end, assert(s), or an early return), give a default with ?? or or, or make the target optional (T?).

Basic mode lets nil into every type, so this code only appears in files under --!strict or check.mode = "strict".

Example

× failswowlua
--!strictlocal function first(xs: string[]): string?    return xs[1]endlocal s = first({ "a" })print(s:upper())
✓ compileswowlua
--!strictlocal function first(xs: string[]): string?    return xs[1]endlocal s = first({ "a" })if s then    print(s:upper())end

The compiler reports it like this:

error[E2005]: `string?` may be `nil`
  ┌─ src/main.wowlua:6:9
  │
6 │ print(s:upper())
  │         ^^^^^ here
codemessage
E2001Type mismatch: S is not assignable to T

Offline

terminal
$ wowlua explain E2005
E2005 (error): Value may be nil (strict mode)

source on GitHub ↗