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

Confusion: arg and ...

Started by HDeffo, 15 May 2015 - 04:37 PM
HDeffo #1
Posted 15 May 2015 - 06:37 PM
So I have noticed not too many people know that you dont need to define arg in a function using … arguments (example below)

//#this does work
foo = function(...)
   print(arg[1])
end
foo("bar")


However my confusion on this is i have recently noticed you cant use both arg and … in a function or else arg is no longer automatically defined for you. Is there an actual reason for this? Because it seems rather random that suddenly something doesnt happen because i chose to use something else. This code will demonstrate what i mean.


print("test one using only arg")
test = function(...)
   print(tostring(arg))
end
test(true)//#table expected and got table. So this is good


print("test two using only ...")
test = function(...)
   print(tostring({...}))
end
test(true)//#table expected and got table. Still doing good

print("test three using both arg and ...")
test = function(...)
   print(tostring(arg))
   print(tostring({...}))
end
test(true)//#two tables expected and we got nil and a table. So now suddenly arg doesnt exist

…If someone could explain this quirk to me please :P/>
Edited on 15 May 2015 - 04:40 PM
Quintuple Agent #2
Posted 15 May 2015 - 06:54 PM
I found a forum post here from StackOverflow that asked the same question.
In short it is the compiler and compatibility with 5.0 and 5.1 lua code. If … is detected it will default to that instead of arg.
flaghacker #3
Posted 15 May 2015 - 06:54 PM
I just tested this on my android device in sime kind of Lua IDE, and it didn't automatically define arg. Could be something odd in the app though.


function func(...)
   print(arg [1]) --error: attempt to index global 'arg'
end 

func ("a", 3)

Edit: Damm, the ninja was smarter then me. ;)/>

Edit2: Is there a shortcut text for the ninja? like : ) -> :)/>

Edit3: :ph34r:/>
Edited on 15 May 2015 - 05:03 PM
Quintuple Agent #4
Posted 15 May 2015 - 06:57 PM
Edit2: Is there a shortcut text for the ninja? like : ) -> :)/>
:ph34r:/> (: ph34r :)
Edited on 15 May 2015 - 04:58 PM
flaghacker #5
Posted 15 May 2015 - 07:02 PM
Edit2: Is there a shortcut text for the ninja? like : ) -> :)/>
:ph34r:/> (: ph34r :)

Thanks, bookmarked that url.
HDeffo #6
Posted 15 May 2015 - 07:19 PM
I just tested this on my android device in sime kind of Lua IDE, and it didn't automatically define arg. Could be something odd in the app though.


function func(...)
   print(arg [1]) --error: attempt to index global 'arg'
end
func ("a", 3)

sometimes emulators have weird behaviors in computercraft it will define it




@Quintuple Agent

thanks that explains it very well actually. Now it makes sense why it seems so random and mildly buggy. I didnt realize the usage of arg was technically deprecated.