Use table variable in Lua class - class

I need a table variable in a Lua class, which is unique for each instance of the class.
In the example listed below the variable self.element seems to be a static variable, that is used by all class instances.
How do I have to change the testClass to get a non static self.element table variable?
Example:
local testClass ={dataSize=0, elementSize=0, element = {} }
function testClass:new (data)
local o = {}
setmetatable(o, self)
self.__index = self
-- table to store the parts of the element
self.element = {}
-- data
self.element[1] = data
-- elementDataSize
self.elementSize = #data
return o
end
function testClass:getSize()
return self.elementSize
end
function testClass:addElement(data)
-- add data to element
self.element[#self.element+1] = data
self.elementSize = self.elementSize + #data
end
function testClass:printElement(element)
-- print elements data
element = element or self.element
local content = ""
for i=1, #element do
content = content .." " .. tostring(element[i])
end
print("Elements: " .. content)
end
function printAll(element)
print("Size: " .. tostring(element:getSize()))
element:printElement()
end
test = testClass:new("E1")
printAll(test)
test:addElement("a")
printAll(test)
test2 = testClass:new("E2")
printAll(test2)
test2:addElement("cde")
printAll(test2)
print("............")
printAll(test)
This implementation returns:
$lua main.lua
Size: 2
Elements: E1
Size: 3
Elements: E1 a
Size: 2
Elements: E2
Size: 5
Elements: E2 cde
............
Size: 3
Elements: E2 cde
For the last output I need
Size: 3
Elements: E1 a

In testClass:new() self refers to testClass.
local test = testClass:new()
test.element will refer to testClass.element
Your instance is o, so when you want each instance to have its own element replace self.element = {} with o.element = {}

You are making a common mistake in your :new function.
You assign self.element but this should be o.element. self in this function refers to the testClass table not to the object you are creating. To make element unique to each object you need to assign it to o, the object being created.
function testClass:new (data)
local o = {}
setmetatable(o, self)
self.__index = self
-- table to store the parts of the element
o.element = {}
-- data
o.element[1] = data
-- elementDataSize
o.elementSize = #data
return o
end

Related

What does this function actually do?

i am currently trying to do some self learning in swift just for my own interest. in the course i bought it says that we should create a function similar to this one in order to solve my problem. but I'm blankly staring trying to figure out what this function actually does?
func unknown() -> () -> Int {
var x = 0
let z: () -> Int = {
x += 1
return x
}
return z
}
It is a function that returns another function which will return an integer that will be increased everytime you call it:
let afunc = unknown()
let value1 = afunc() // 1
let value2 = afunc() // 2
let value3 = afunc() // 3
The interesting part of this is the return type. () -> Int is a function that returns an Int, which means that unknown returns a function rather than something simple, like a number.
z is then a variable of that same type and is assigned a function definition to be returned.
If you assign the result of unknown to a variable, you can then invoke the returned function.
This implementation of a high order function is an interesting way of defining generators. An infinite sequence-like class would've achieve the same thing, but with more verbosity:
class MySequence {
private var x = 0
func unknown() -> Int {
x += 1
return x
}
}
var seq = MySequence()
let unknown = seq.unknown
print(unknown()) // 1
print(unknown()) // 2
print(unknown()) // 3
// ... and so on
The main difference between the class and the anonymous closure is the storage for x: the closure captures in due to using the variables within its body, while the class declares explicit storage for the property.
Some fancy stuff can result by using high order functions, like a generator for the Fibonacci numbers:
func fibonnaciSequence() -> () -> Int? {
var a = 0, b = 1
return { let c = a; a += b; b = c; return c }
}
let fibo = fibonnaciSequence()
while let f = fibo() {
// this will print forever
// actually not forever, it will stop at some point due to += overflowing
print(f)
}

Optional function arguments with no default value possible?

In Chapel, we can set the default value of function formal arguments easily, for example,
proc test( a = 1, b = 2.0, c = "hi" ) {
...
}
and call the function by using keywords also:
test( 10 ); // a = 10, b = 2.0, c = "hi"
test( b = 3.14 ); // a = 1, b = 3.14, c = "hi"
test( c = "yo" ); // a = 1, b = 2.0, c = "yo"
Here, I am wondering if it is possible to define a keyword argument that does not require a predefined default value. More specifically, I would like to write a function that can optionally receive an array depending on cases (e.g., to save intermediate data). Here, the only requirement is that I can check whether the actual argument is passed or not, and there is no need to give the default array value. I imagined something like
proc test( ..., optional d: [] real ) {
if present( d ) then ...;
}
or
proc test( ..., d: [] real = None ) {
if present( d ) then ...;
}
but was not able to find similar things. At the moment, my workaround is to give some dummy default value and check their properties to determine whether an actual argument is passed.
proc test( arr = empty2Dreal ) { ... } // where "empty2Dreal" is a pre-defined global array
or
proc test( arr = reshape( [0.0], {1..1,1..1} ) ) { ... } // some dummy array
}
However, I am wondering whether there might be a more elegant(?) or idiomatic(?) approach...
Edit
As suggested in the comment, it is also convenient to overload several functions to get different interfaces, but at some point I guess I need to pass some "dummy" object to the final (full-fledged) routine and ask the latter to see if the passed object is "dummy" or not... MWE is something like this:
const empty1Dint: [1..0] int;
proc test( x: real, arr: [] int )
{
writeln("test() with 2 args");
writeln(( x, arr ));
// here, I need to check whether the passed object is
// an actual array or not by some predefined rule
if arr.size > 0 then writeln("got a non-empty array");
}
proc test( x: real )
{
writeln("test() with 1 arg");
test( x = x, arr = empty1Dint );
}
var work = [1,2,3,4,5];
test( x = 1.0 );
writeln();
test( x = 1.0, arr = work );
which gives
test() with 1 arg
test() with 2 args
(1.0, )
test() with 2 args
(1.0, 1 2 3 4 5)
got a non-empty array
The corresponding default-value version is
const empty1Dint: [1..0] int;
proc test( x: real, arr: [] int = empty1Dint )
{
writeln("test() with 2 args");
writeln(( x, arr ));
if arr.size > 0 then writeln("got a non-empty array");
}
var work = [1,2,3,4,5];
test( x = 1.0 );
writeln();
test( x = 1.0, arr = work );
which gives
test() with 2 args
(1.0, )
test() with 2 args
(1.0, 1 2 3 4 5)
got a non-empty array
Although the above approach works for arrays, the rule needs to change depending on the type of objects used. So, I was wondering if there is some systematic way, e.g., to pass a "null pointer" or some unique global object to tell the final routine about the presence of the actual data. (But, as noted above, the above approach works for arrays).
Edit 2
Another approach may be simply to pass an additional flag for using the passed array (then there is no need to think much about the nature of the default object, so may be overall simpler...)
const empty1Dint: [1..0] int;
proc test( x: real, arr: [] int = empty1Dint, use_arr = false )
{
writeln( "x= ", x );
if use_arr {
writeln("working with the passed array...");
for i in 1..arr.size do arr[ i ] = i * 10;
}
}
test( x = 1.0 );
writeln();
var work: [1..5] int;
test( x = 2.0, arr = work, use_arr = true );
writeln( "work = ", work );
Edit 3
Following Option 3 in the answer, here is a modified version of my code using _void and void:
proc test( x: real, arr: ?T = _void )
{
writeln( "\ntest():" );
writeln( "x = ", x );
writeln( "arr = ", arr );
writeln( "arr.type = ", arr.type:string );
writeln( "T = ", T:string );
if arr.type != void {
writeln( "doing some checks" );
assert( isArray( arr ) );
}
if arr.type != void {
writeln( "writing arr" );
for i in 1..arr.size do arr[ i ] = i * 10;
}
}
// no optional arg
test( x = 1.0 );
// use an optional arg
var work: [1..5] int;
test( x = 2.0, arr = work );
writeln( "\nmain> work = ", work );
Result:
test():
x = 1.0
arr =
arr.type = void
T = void
test():
x = 2.0
arr = 0 0 0 0 0
arr.type = [domain(1,int(64),false)] int(64)
T = [domain(1,int(64),false)] int(64)
doing some checks
writing arr
main> work = 10 20 30 40 50
This answer discusses 3 answers:
The strategy discussed in the edit of the question.
A strategy using a Box type
A strategy using a generic function with a void default value
My favorite of these options is Option 3.
Option 1
proc test( x: real, arr: [] int = empty1Dint, use_arr = false ) strategy described in the question is reasonable, if a little verbose. The main drawback here is that you'd need more overloads of test if you didn't want the call sites to have to pass use_arr=true or use_arr=false. Here is a simple program that does that:
proc test(optional, hasOptional:bool) {
writeln("in test");
writeln(" optional is ", optional);
if hasOptional == false then
writeln(" note: default was used for optional");
}
proc test(optional) {
test(optional, hasOptional=true);
}
proc test() {
var emptyArray:[1..0] int;
test(emptyArray, hasOptional=false);
}
test();
test([1, 2, 3]);
Option 2
Another alternative is to create a class to store the optional argument data, and pass nil by default.
class Box {
var contents;
}
proc makeArray() {
var A:[1..2] int;
return A;
}
proc emptyBox() {
var A:[1..0] int;
var ret: owned Box(A.type) = nil;
return ret;
}
proc test( optional=emptyBox() ) {
writeln("in test with optional=", optional);
}
test();
test(new owned Box(makeArray()));
Here the main tricky part is that the array type returned by makeArray() and emptyBox() have to match. It'd be possible to use a type alias to have them refer to the same array type, but how exactly that would fit in depends on your application. Another problem with this approach is that it causes the array to be copied in the process of passing such an argument. And, one has to think about where the Box will be destroyed. Is test to hang on to the array value (e.g. storing it in a data structure) or just going to use it temporarily? This is set by the type returned by emptyBox in my example.
It's probably reasonable for the standard library to gain such a Box type but it doesn't have one now.
Option 3
My favorite solution to this problem is a third strategy altogether.
Chapel includes a value of void type called _void. The key is the declaration proc test( optional:?t=_void ). Here test is a generic function - the syntax argument:?t indicates that the argument can have a varied type (which will be available as t within the function). This is necessary to get a generic argument that also has a default value (otherwise the argument will have only the type inferred from the default value).
If no optional argument is provided, it will instantiate with optional having type void. Which makes sense as a way to not pass something. Technically it's not the same as checking if the default value was provided, but I think a call site like test(optional=_void) is reasonably clear at communicating that the value of optional should be ignored (since it's void).
Anyway here is the code:
proc test( optional:?t=_void ) {
writeln("in test");
writeln(" optional is ", optional);
if optional.type == void then
writeln(" note: default was used for optional");
}
test();
test([1, 2, 3]);

How to get the string representation of a nested class

In my reflection library EVReflection I have the following problem when class definitions are nested (class within a class). Below is a worked out case which can be found as a unit test here and The location in the library itself where the code needs to change is Here
I need to get the Internal Swift string representation of a nested
class for a property which is an array of that nested class.
Below you can see a unit test where I am able to get the correct type for the property company that is an other object. It will output _TtCC22EVReflection_iOS_Tests13TestIssue114b10Company114 instead of Company114
When I try the same for the friends property my goal is that it outputs something like: Swift.Array<_TtCC22EVReflection_iOS_Tests13TestIssue114b7User114>
What do I have to do to get that?
As you can see in the test I have various assignments to the value valueType. None of these assignments work. I am only able to get an Array<User114> or an Swift._EmptyArrayStorage.
As you also can see in the test is that if I set a breakpoint and do a po in the output window I am able to get the correct output. So what code will accomplish the same in my code?
class TestIssue114b: XCTestCase {
class User114: EVObject {
var company: Company114 = Company114()
var friends: [User114] = []
}
class Company114: EVObject {
var name: String = ""
var address: String?
}
func testIssueNestedObjects() {
let x = User114()
print("type 1 = \(NSStringFromClass(type(of: x.company)))") // output = type 2 = _TtCC22EVReflection_iOS_Tests13TestIssue114b10Company114
print("type 2 = \(testIssueNestedObjects(x.friends))")
}
func testIssueNestedObjects(_ theValue: Any) -> String {
var valueType = ""
let mi = Mirror(reflecting: theValue)
valueType = NSStringFromClass(type(of: (theValue as! [NSObject]).getTypeInstance() as NSObject)) // NSObject
valueType = "\(type(of: theValue))" // Array<User114>
valueType = "\(mi.subjectType)" // Array<User114>
valueType = ObjectIdentifier(mi.subjectType).debugDescription //"ObjectIdentifier(0x0000000118b4a0d8)"
valueType = (theValue as AnyObject).debugDescription // <Swift._EmptyArrayStorage 0x10d860b50>
valueType = NSStringFromClass(type(of: theValue as AnyObject)) // Swift._EmptyArrayStorage
// set breakpont en enter this in output window: (lldb) po type(of: theValue)
// Ouput will be: Swift.Array<EVReflection_iOS_Tests.TestIssue114b.User114>
return valueType
}
}
Background info:
Actually the end goal is that I have to be able to create instances of the object that I can add to the array. Since the array property is only available as a result from a Mirror command the variable will be of type Any. I do have an extension for arrays in place that will return a new array element. however I am only able to get that when the Any is casted to Array<NSObject> and because of that my extension will return an NSObject. So I would like to get a string like Swift.Array<_TtCC22EVReflection_iOS_Tests13TestIssue114b7User114> I can then get the parts between <> and then create an instance for that using NSClassFromString.
String(reflecting: type(of: theValue))
update by Edwin Vermeer:
For the required conversion to the internal string representation i now have the following function (still in draft)
public class func convertToInternalSwiftRepresentation(type: String) -> String {
if type.components(separatedBy: "<").count > 1 {
// Remove the Array or Set prefix
let prefix = type.components(separatedBy: "<") [0] + "<"
var subtype = type.substring(from: prefix.endIndex)
subtype = subtype.substring(to: subtype.characters.index(before: subtype.endIndex))
return prefix + convertToInternalSwiftRepresentation(type: subtype) + ">"
}
if type.contains(".") {
var parts = type.components(separatedBy: ".")
if parts.count == 2 {
return parts[1]
}
let c = String(repeating:"C", count: parts.count - 1)
var rv = "_Tt\(c)\(parts[0].characters.count)\(parts[0])"
parts.remove(at: 0)
for part in parts {
rv = "\(rv)\(part.characters.count)\(part)"
}
return rv
}
return type
}

function of object in nested table syntax?

I got a table like this which is giving me
'(' expected near 't' at 'errorline'
Which means there must be an syntax error, but I can't detect one. Have you any idea what's wrong with the syntax?
t = {}
t[x] = {
some = "data",
foo = function() return "bar" end,
elements = { -- the class is working 100%, have used it for several projects.
mon = class:new(param),
tue = class:new(param2),
n = class:new(param3),
},
function t[x].elements.mon:clicked() -- <<< --- ERRORLINE
--dosomething
end,
}
Add the function t[x].elements.mon:clicked() after the table declaration i.e after the closing braces of the table .
t = {}
t[x] = {
some = "data",
foo = function() return "bar" end,
elements = { -- the class is working 100%, have used it for several projects.
mon = class:new(param),
tue = class:new(param2),
n = class:new(param3),
}
}
t[x].elements.mon.clicked = function(self)
--dosomething
end
EDIT :
As mentioned in the comments function t[x].elements.mon:clicked() wont work.
Function declaration should be t[x].elements.mon.clicked = function(self).
Note that the first parameter for the function would be self if you call a dot function using colon. i.e If you call the function as c = t[x].elements.mon:clicked(a,b) then the function should be
t[x].elements.mon.clicked = function(self,a,b)

Lua custom linked list for class creation practice fails to set node.next

So I've been trying to figure out how to mimic classes in lua. So I could start with some basic code that I already have written in other languages, I started with a linked list. I have what seems to be a working node class, but when i try to print out the list from my LList class, I cant seem to get it to print.
There seems to be a problem where after the first node is added to the list, either the second node that is added gets overwritten by the third, or it simply doesn't actually get added. This could be a problem with a while loop I'm using, but I have no idea why it would be an issue in this case.
Here is the Linked List "class"
LList = {}
LList.__index = LList
function LList.create()
local list = {} -- our new object
setmetatable(list,LList) -- make LList handle lookup
list.count = 0 -- initialize our object
list.head = nil
return list
end
function LList:add(newNode)
print("DEBUG PRINT: LList:add(): newNode.data: "..newNode:getData().." LList.count: "..self.count)
if(self.head) then
local curr = self.head
print("DEBUG PRINT: LList:add(): self.head:toString(): "..self.head:toString())
print("DEBUG PRINT: LList:add(): curr:toString(): "..curr:toString())
print("DEBUG PRINT: LList:add(): newNode:toString: "..newNode:toString())
while curr.nextNode do --this is the while loop in question
print("DEBUG PRINT: LList:add(): in while:"..curr:toString())
curr = curr.nextNode
end
curr:setNext(newNode)
print("DEBUG PRINT: LList:add(): curr.nextNode:toString(): "..curr.nextNode:toString())
self.count = self.count + 1
else
self.head = newNode
self.count = 1
print("DEBUG PRINT: LList:add(): self.count" .. self.count .." self.head:getData(): ".. self.head:getData())
end
print("DEBUG PRINT: LList:add(): EXITING\n")
end
function LList:getLen()
return self.count
end
function LList:toString()
print("Stubbed toString()")
if(head)then
print(self:toStringHelper(head))
else
print("emptyList")
end
end
function LList:toStringHelper(currNode)
if(currNode.nextNode)then
return currNode:toString() .. toStringHelper(currNode.nextNode)
else
return currNode:toString()
end
end
This here is the Node "class"
Node = {}
Node.__index = Node
function Node.create(newData)
local tNode = {}
setmetatable(tNode, Node)
tNode.data = newData
return tNode
end
function Node:getData()
return self.data
end
function Node:getNext()
return self.nextNode
end
function Node:setNext(newNode)
self.nextNode = newNode
print("DEBUG PRINT: Node:setNext(): self.nextNode:toString(): "..self.nextNode:toString())
end
function Node:hasNext()
if self.nextNode then
return true
else
return false
end
end
function Node:toString()
return tostring(self.data)
end
This is the tester code. Again, all code is in one file for ease of testing.
testerList = LList.create()
print(testerList:getLen())
tNode1=Node.create(5)
tNode2=Node.create(7)
tNode3=Node.create(2)
testerList:add(tNode1)
testerList:add(tNode2)
testerList:add(tNode3)
print(testerList:getLen())
print(testerList:toString())
I believe that my problem is either in the while loop implementation, or in the way I'm setting the Node.nextNode data.
Works for me after fixing some typos in your code:
In LList:toString() you need to replace the references to head with self:head and in LList:toStringHelper(currNode) you need to replace the references to toStringHelper with self:toStringHelper. After this the code prints out the correct list.