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

[Lua][Question] Parsing Function Pointer

Started by theoriginalbit, 18 December 2012 - 02:26 AM
theoriginalbit #1
Posted 18 December 2012 - 03:26 AM
Ok so function pointers. Easily parsed around. example


function doSomething( func )
  func()
end

doSomething( turtle.forward )

ok so here is my problem.


-- This is in an api
function doStuff( hashFunc, accept )
  -- do some stuff from this api
  return hashFunc( string ) == accept
end

function runAPI( something, accept, hashFunc )
  -- do stuff here
  doStuff( hashFunc )
end


-- this is in the program that calls the api
function hashTheString()
  -- hash it
  return hash
end

function runProgram()
	a = hashTheString()
	api.runAPI( something, a, hashTheString )
end

would this work?

i know that the following works


function runProgram()
  a = hashTheString()
  api.runAPI( something, a, anotherAPI.hashTheString )
end
CoolisTheName007 #2
Posted 18 December 2012 - 04:48 AM
Can you describe the problem better? And, apis are loaded as functions, which are called, and then their environment is shallow copied.
Lyqyd #3
Posted 18 December 2012 - 04:57 AM
Yep, that should perform as expected.
theoriginalbit #4
Posted 18 December 2012 - 10:40 AM
Can you describe the problem better? And, apis are loaded as functions, which are called, and then their environment is shallow copied.

I don't think i could explain it better :P/> The problem that I have is I've found that parsing functions without the ( ) don't always work, especially when they have a variable named the same.


Yep, that should perform as expected.

Ok cool. how about when they have a variable with the same name? it will error wont it…
CoolisTheName007 #5
Posted 18 December 2012 - 10:59 AM
Defining functions has to be done with the (), so I don't understand how would you not use () in order to parse correctly a function definition, but calling them doesn't have to be. i.e. if the arguments are string or tables, e.g. require'mypath'{var1=2} is as good as require('mypath',{var1=2}). Maybe you are referring to something related to this last case? Even if you did, as soon as the function name is overridden by a local variable (i.e. defined in the function main chunk or passed as a parameter), the variable name points to the new variable value.

local b=2--or b=2, same result

function a(B)/>/>/>/>
print(B)/>/>/>/>
end
end
a()
--prints nothing
a(3)
--prints 3


function a(a)
print(a)
end
a()--prints nothing
a(2)--prints 2
Damn editor, that B is a b.
theoriginalbit #6
Posted 18 December 2012 - 11:23 AM
Defining functions has to be done with the (), so I don't understand how would you not use () in order to parse correctly a function definition, but calling them doesn't have to be. i.e. if the arguments are string or tables, e.g. require'mypath'{var1=2} is as good as require('mypath',{var1=2}). Maybe you are referring to something related to this last case? Even if you did, as soon as the function name is overridden by a local variable (i.e. defined in the function main chunk or passed as a parameter), the variable name points to the new variable value.

local b=2--or b=2, same result

function a(B)/>/>/>/>/>/>/>
print(B)/>/>/>/>/>/>/>
end
end
a()
--prints nothing
a(3)
--prints 3


function a(a)
print(a)
end
a()--prints nothing
a(2)--prints 2
Damn editor, that B is a b.

you cannot parse pass a function and use the ( ) or else it will call the function and parse pass its return value. you must parse pass a function pointer ( without the ( ) ) to the function wanting it which then adds the ( ) to the end.
CoolisTheName007 #7
Posted 18 December 2012 - 11:24 AM
parse or pass? I guess my vocabulary is incomplete, I really don't think that's what parsing means
Ok parse== the interpreter converting code into stuff, I get it….
But from reading your code, I don't see any problem!!There's no mention of what you want to do, an error…
"would this work?"
did you try?
theoriginalbit #8
Posted 18 December 2012 - 11:27 AM
sorry pass. typing quick and not thinking :P/>

EDIT: At least i was consistent :P/>
theoriginalbit #9
Posted 18 December 2012 - 01:31 PM
[SOLVED] I have one line that makes sure of they parse a variable its a controlled quit that tells them exactly what went wrong, instead of an error


if ( type( passedFunc ) ~= "function" ) then
  -- print out a lovely error message
  error()
end

EDIT: It also means that they won't be posting saying that they get an error that says "tried to call string" or whatever variable they pass in :)/> and trying to say its my fault :P/>
CoolisTheName007 #10
Posted 18 December 2012 - 10:12 PM
[SOLVED] I have one line that makes sure of they parse a variable its a controlled quit that tells them exactly what went wrong, instead of an error


if ( type( passedFunc ) ~= "function" ) then
  -- print out a lovely error message
  error()
end

EDIT: It also means that they won't be posting saying that they get an error that says "tried to call string" or whatever variable they pass in :)/> and trying to say its my fault :P/>

Even better:

if ( type( passedFunc ) ~= "function" ) then
  -- print out a lovely error message
  error('Wrong argument: arg1 should be function',2)
end
This will make Lua error on the call to your function, i.e. print the line of the user's code that's using your function with the wrong args.
There's also a way to shorten all of this:

assert(type(passedFunc)=='function','Wrong argument: arg1 should be function')
See this
theoriginalbit #11
Posted 18 December 2012 - 10:19 PM
[SOLVED] I have one line that makes sure of they parse a variable its a controlled quit that tells them exactly what went wrong, instead of an error


if ( type( passedFunc ) ~= "function" ) then
  -- print out a lovely error message
  error()
end

EDIT: It also means that they won't be posting saying that they get an error that says "tried to call string" or whatever variable they pass in :)/> and trying to say its my fault :P/>

Even better:

if ( type( passedFunc ) ~= "function" ) then
  -- print out a lovely error message
  error('Wrong argument: arg1 should be function',2)
end
This will make Lua error on the call to your function, i.e. print the line of the user's code that's using your function with the wrong args.
There's also a way to shorten all of this:

assert(type(passedFunc)=='function','Wrong argument: arg1 should be function')
See this

what is the " , 2 " after the message in the first one?


EDIT: Ahhh nice. changing to assert
CoolisTheName007 #12
Posted 18 December 2012 - 10:23 PM
what is the " , 2 " after the message in the first one?
You should google more: http://www.lua.org/pil/8.5.html
It's the 3rd paragraph.
theoriginalbit #13
Posted 18 December 2012 - 10:29 PM
what is the " , 2 " after the message in the first one?
You should google more: http://www.lua.org/pil/8.5.html
It's the 3rd paragraph.

oh wow I must have missed that whole page when i went through those docs. lol
theoriginalbit #14
Posted 18 December 2012 - 10:53 PM
I've Googled and tested… now I'm asking… is there a way of doing a Level 2 assert?

I tried these, and both of them trigger the error.


assert(key ~= nil, error("A key must be provided when registering a new render mode.", 2))
assert(key == nil, error("A key must be provided when registering a new render mode.", 2))
CoolisTheName007 #15
Posted 18 December 2012 - 10:56 PM
I've Googled and tested… now I'm asking… is there a way of doing a Level 2 assert?

I tried these, and both of them trigger the error.


assert(key ~= nil, error("A key must be provided when registering a new render mode.", 2))
assert(key == nil, error("A key must be provided when registering a new render mode.", 2))
Assert is level 2 by default.
You are actually throwing an error before calling assert, because the args of assert are being evaluated first.
theoriginalbit #16
Posted 18 December 2012 - 10:57 PM
I've Googled and tested… now I'm asking… is there a way of doing a Level 2 assert?

I tried these, and both of them trigger the error.


assert(key ~= nil, error("A key must be provided when registering a new render mode.", 2))
assert(key == nil, error("A key must be provided when registering a new render mode.", 2))
Assert is level 2 by default.

hmmm thats strange then, because even when i do it like this


assert(key ~= nil, "A key must be provided when registering a new render mode.")

It points the finger at my code instead of where its called…. :/
theoriginalbit #17
Posted 18 December 2012 - 11:02 PM
odd. if I replace key == nil with type(key) == nil it works

EDIT: wait, no I'm wrong, still doesn't do level 2. :(/>
CoolisTheName007 #18
Posted 18 December 2012 - 11:12 PM
odd. if I replace key == nil with type(key) == nil it works
If the condition is false, it raises the error. I'm confused by you using two opposite statements. That way it always throws an error.
theoriginalbit #19
Posted 18 December 2012 - 11:14 PM
odd. if I replace key == nil with type(key) == nil it works
If the condition is false, it raises the error. I'm confused by you using two opposite statements. That way it always throws an error.

i was stating that I tried both ways first before posting about it, just to i guess "triple check".
CoolisTheName007 #20
Posted 18 December 2012 - 11:33 PM
snip
Apparently, assert does not raise level 2 errors. My bad.
theoriginalbit #21
Posted 18 December 2012 - 11:38 PM
snip
Apparently, assert does not raise level 2 errors. My bad.

ok so to get level 2 I guess its back to "if then error" 's then