Groups - jpbubble/NIL-isn-t-Lua GitHub Wiki

Groups

A group is a kind of sub-class in NIL. You cannot assign any variables to it, like you can do with a normal class, and the variable you assign with it will carry that class. Technically you can get the same effect by making a class making all its members static, but in a group this is always the case.

group HelloWorld

	void Go()
		print("Hello World")
	end

end

HelloWorld.Go()

Advantage of groups is that they "force" you to keep your code a bit cleaner, without having to make tons of classes and assigning variables to them... Technically NIL does that all in one go. Like regular classes groups can use constructors

group MyGroup                                                                                       
                                                                                                    
        string Name                                                                                 
        string YearOfBirth                                                                          
                                                                                                    
        void CONSTRUCTOR()                                                                          
                MyGroup.Name = "Jeroen"                                                             
                MyGroup.YearOfBirth = 1975                                                          
        end                                                                                         
                                                                                                    
        void Go()                                                                                   
                print("Hello! I am "..MyGroup.Name.." and I was born in "..MyGroup.YearOfBirth..".")
        end                                                                                         
                                                                                                    
end                                                                                                 

MyGroup.Go()

Important Note!

NIL's parser will not throw any errors when you make your constructor ask for parameters, but as NIL will never provide them, it will cause an error during runtime. Cut short, it should never be done.

Alternate Note!

Alternatively the keyword "self" is supported by groups in the same manner as in classes. So this code has effectively the same effect as the code above

group MyGroup                                                                                       
                                                                                                    
        string Name                                                                                 
        string YearOfBirth                                                                          
                                                                                                    
        void CONSTRUCTOR()                                                                          
                self.Name = "Jeroen"                                                             
                self.YearOfBirth = 1975                                                          
        end                                                                                         
                                                                                                    
        void Go()                                                                                   
                print("Hello! I am "..MyGroup.Name.." and I was born in "..MyGroup.YearOfBirth..".")
        end                                                                                         
                                                                                                    
end                                                                                                 

As locals are faster than globals, using "self" may even speed things up a little. Groups can be made private or local by typing "private group" in stead of "group"