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

Implementing an isMethod() function

Started by Link149, 09 July 2014 - 01:04 AM
Link149 #1
Posted 09 July 2014 - 03:04 AM
Hello everyone, I was wondering if you knew a way to find out whether a function has been written with the colon ':' notation or not. It's okay if it's not "ComputerCraft-compatible" as I'm working on a OO engine outside of ComputerCraft, but I guess it's a plus if it does. I thought I could use the 'debug' library to get the number of arguments a function received, when calling it with no arguments but I've never really used this library, so I'm not really used to it. I'm pretty sure I've heard somwhere that it is a bit slow.

Anyway, thanks in advance. ;)/>

I'll keep looking by myself and let you know if I come across anything.
Lyqyd #2
Posted 09 July 2014 - 03:13 AM
Moved to Ask a Pro.
Link149 #3
Posted 09 July 2014 - 03:26 AM
Oh, I thought I had created the topic in Ask a Pro. Sorry. :unsure:/>
theoriginalbit #4
Posted 09 July 2014 - 03:31 AM
well no the isn't really a way to tell if a function as been declared with colon notation, and this is why

local t = {}

--# with colon notation
function t:foo()
  print(self)
end

--# without colon notation
function t.foo(self)
  print(self)
end

invoking a function with OO in Lua is possible with any function, even this one

local t = {}
function t.bar()
  print("I'm just ignoring arguments given to me")
end
t:bar()

it is possible for you to be able to make non-OO calls into OO calls with some metatable magic, but it is by no means fast. but it does mean that no matter how the function is invoked it will work as an OO invocation. So assuming we have a function `foo` in the table `t` we can invoke it like so

t:foo()
t.foo(t)
t.foo()
Edited on 09 July 2014 - 01:32 AM
oeed #5
Posted 09 July 2014 - 11:04 AM
It's also worth noting the CC doesn't have the debug API enabled.

What are you trying to achieve? It might be possible, or maybe even better, if you were to code it yourself. I've learnt tons about OOP through making the OO systems in PearOS (granted, it was rather flawed) and OneOS.
InputUsername #6
Posted 10 July 2014 - 12:38 AM
It's also worth noting the CC doesn't have the debug API enabled.

What are you trying to achieve? It might be possible, or maybe even better, if you were to code it yourself. I've learnt tons about OOP through making the OO systems in PearOS (granted, it was rather flawed) and OneOS.

(…) It's okay if it's not "ComputerCraft-compatible" as I'm working on a OO engine outside of ComputerCraft, but I guess it's a plus if it does. (…)

Also, as far as I know there's no way to tell the difference, as you're basically doing the same thing (calling a function) in three different(-looking) ways. I believe I've even read somewhere all methods (no pun intended) give very similar bytecode, but I could be wrong.
Link149 #7
Posted 10 July 2014 - 12:40 AM
It's also worth noting the CC doesn't have the debug API enabled.

Yeah, I knew about that, but as I said, it's okay if it's not ComputerCraft compatible.
I was trying to distinguish methods from functions, mainly for debug purposes but
I think I found a better way to do this anyway.
Link149 #8
Posted 11 July 2014 - 01:41 AM
it is possible for you to be able to make non-OO calls into OO calls with some metatable magic, but it is by no means fast. but it does mean that no matter how the function is invoked it will work as an OO invocation. So assuming we have a function `foo` in the table `t` we can invoke it like so

t:foo()
t.foo(t)
t.foo()

That's exactly how I intended to do it, but thanks anyway. :D/>
natedogith1 #9
Posted 13 July 2014 - 03:14 PM
If you still care about it and are willing to look a bit at low-level lua (hello there byte-code); you could try dumping the function(string.dump) and seeing if it has a self variable as its first argument. (I think this means the first local variable is named self) If you're interested check out this http://luaforge.net/...nstructions.pdf