Support for recursive objects in lua

I am new to lua and have the following issue with assignment from class:

We are currently extending lua to support objects and inheritance. Syntax for this

Class{'MyClass', 
    attribute1 = String,
    attribute2 = Number
}

Class{'MySubClass', MyClass,
    attribute3 = Number
}

This works great. The real problem is the following task: we must support "recursive types", which means a type call

Class{'MyClass', attribute = MyClass}

should result in a class with a field of the same type as the class. When this "constructor class" is called MyClassequal nil, so the parameter table has no entry attribute. How can I access this attribute?

- nil -table, , __index . nil -table nil, "-". , , nil == unknown. true, - __eq nil -table , true.

, ? .

.

: " ". , , .

three = 3
print( three  == 3 , "Should be true")
print( unknown == nil , "Should be true" )

Class{'AClass', name = String, ref = AClass}
function AClass:write()
    print("AClass:write(), name of AClass:", self.name)
end

aclass = AClass:create("A. Class")
aclass:write()
+4
2

MyClass - (_G), __index, MyClass ( ).

, ,

  • , undefined (, , . , )
  • , _G nil ( , , )

, __newindex.

+3

?

Class{'MyClass', attribute = 'MyClass'}

Class _G[string]

, , :

Class{'MyClass', attribute = function() return MyClass end}
+3

All Articles