the with keyword - jpbubble/NIL-isn-t-Lua GitHub Wiki

The "with" keyword is not available in standard Lua, but it is to NIL....

Do you hate this notation?

class MyClass
   int a
   int b
   int c
   int d
end

var ClassVar
ClassVar = new MyClass
ClassVar.a = 1
ClassVar.b = 2
ClassVar.c = 4
ClassVar.d = 8

You don't wanna do that, do you? The "with" keyword takes that trouble away from you

class MyClass
   int a
   int b
   int c
   int d
end

var ClassVar
ClassVar = new MyClass
with ClassVar
  $a = 1
  $b = 2
  $c = 4
  $d = 8
end

Cool, huh? Please note that "with" creates a new scope, so this means that variables declared between "with" and "end" count as locals to that "with" scope. Scopes within the with scope will take over the 'with' of their parent so this

with ClassVar
    if i==1
      $a = 5
    end
end

Will have the same effect as

if i==1
   Classvar.a=5
end

When putting a with inside a with, only the with of the "deepest" scope counts, so with this:

with w1
   with w2
      print($a) // translates to w2.a
   end
   print($a) // translates to w1.a
end

Within class methods and groups, the "$" will automatically become "self" in every method scope, in the case of static methods, "self" will mean the classname itself. When using "$" without any with scopes or being inside class/group methods methods "$" translates as "_G", and will therefore look for globals, although this was merely error prevention, as "$" without using with or methods, is something I consider as "bad practice" which should be avoided, unless you really are sure of yourself that you know what you are doing.