I want to make class like that:
class Example
field: false --some field shared for all instances of the class
init: (using field) ->
field = true --want to change value of the static field above
But in lua I got:
<...>
field = false,
init = function()
local field = true //Different scopes of variable field
end
<...>
In docs I read that writing using helps to deal with it
You can change the value as you described by editing the metatable from the instance:
I don't recommend doing that, class fields are probably what you want to use:
When assigning a class field you can prefix with
@
to create a class variable. In the context of a method,@@
must be used to refernece the class, since@
represents the instance. Here's a brief overview of how@
works:Also, your use of
using
is incorrect.field
is not a local variable. It's a field on the classes's instance meta table.