Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to self-reference table during initialization

Tags:

lua

lua-table

Is there a shorter way to do this:

local thisismytable = {
    non = sequitur
}
thisismytable.whatismytable = thisismytable

Any help would be appreciated. I don't want to re-create pre-existing functionality.

like image 896
SideCode Avatar asked Sep 12 '15 14:09

SideCode


1 Answers

No.

If you can stand the difference between these two expressions thisismytable:whatismytable() instead of thisismytable.whatismytable, you could do:

local thisismytable = {
    non = sequitur,
    whatismytable = function (self) return self end
}

Testing:

print(thisismytable)
print(thisismytable:whatismytable())

More usage:

print(thisismytable:whatismytable().non)
like image 189
Tom Blodget Avatar answered Oct 03 '22 00:10

Tom Blodget