This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Creator's profile picture

Need some help with translating C++ code into Lua.

Started by Creator, 07 October 2015 - 02:58 PM
Creator #1
Posted 07 October 2015 - 04:58 PM
I was transcoding a functioning neural network in Lua from C++. I then came across this:


class Neuron;
typedef vector<Neuron> Layer;

Neuron and Layer are both classes.

The original code is here.
Exerro #2
Posted 07 October 2015 - 05:01 PM
Well, you can't. Not directly at least. Lua offers no inbuilt class construct, so you'll have to make your own (or use someone else's).

As for the typedef, it's basically saying that when you write 'Layer' you're actually writing 'vector<Neuron>', which in Lua would just be a table of Neuron objects.
Creator #3
Posted 07 October 2015 - 05:07 PM
Thank you a lot!
Lignum #4
Posted 07 October 2015 - 05:31 PM
Since that class is just a forward declaration:

local Neuron

But for the real class, you're going to need to follow awsumben13's advice.
Creator #5
Posted 07 October 2015 - 05:34 PM
My question was rather about the second second line.
Lupus590 #6
Posted 07 October 2015 - 05:44 PM
While Lua doesn't have classes, it does 'have' objects.

In C++ you define an object using a class and then make an instance of that class to use it.
In Lua you create a table and replicate an object by placing your variables (attributes) and functions (methods) inside the table (object). you have to be careful with the functions as they have to be written and called in a special way. (I'll get back to this in a moment)

In C++ you have the keyword 'this' to allow an object to refer to itself. Lua uses a variable called 'self' for the same thing.

Back to defining functions/methods, in order for a method to access it's parent object (and anything within) it has to have a pointer to it's table (C++ does this without telling you, but we have to do this ourselves in Lua). There are two ways to do so:

--#creating an object
obj = {}
obj.data = "this is a string"

function obj.m1(self, ...) --#here we use the self as lua won't set it for us, we could call it anything else. For compatibility the object is the first parameter
  print(self.data) --#self.data is the same as obj.data as self is obj
end

function obj:m2(...) --#here we used a colon instead of a dot, this means that Lua will make the self variable for us. The object is the first parameter but Lua is adding that in for us
  print(self.data)
end

--#calling object methods
--#both of the above methods do the same thing in nearly the same way (the colon is shorthand for us, and will be again)

obj.m1(obj) --#we used a dot so we have to pass the obj table so that the object knows what it is
obj:m1() --#this time we use a colon so lua is passing the table without us having to do so.
obj:m1(obj) --#we just passed obj twice (with lua doing it first), while this may not cause problems with the method, it probably is not what we want to do

obj.m2(obj)--#back to using dot just to prove that both calls work with both functions
obj:m2() --#colon works here too
Edited on 07 October 2015 - 03:45 PM
Creator #7
Posted 07 October 2015 - 06:37 PM
Thank you, although I kinda knew thew this, Still, eat a +1 for the effort.
Lupus590 #8
Posted 07 October 2015 - 07:28 PM
Here is something that may make life easier and/or more confusing: http://pastebin.com/c5xqMUb9
Yevano #9
Posted 08 October 2015 - 06:38 PM
Here is something that may make life easier and/or more confusing: http://pastebin.com/c5xqMUb9

Try to be careful with the extent to which you use metatable magic in Lua to make your code pretty. In my experience, it will make your code slower, since whenever the VM does a table lookup it has to do extra work calling your metamethods first. The OO paradigms facilitated by metatables in Lua is certainly cool (and I myself am guilty of spending way too much time playing with various ideas related to it), but in the end Lua offers no way of making this stuff fast. I think if you're just doing the normal inheritance code, you'll probably be fine.

I mention this because I've been seeing a general movement on the forums towards using these types of approaches more and more, and if you're working on something like a neural network, I would think you'd want the result to be as fast as possible.