Jump to content




Confusion: arg and ...


  • You cannot reply to this topic
5 replies to this topic

#1 HDeffo

  • Members
  • 214 posts

Posted 15 May 2015 - 05: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 by HDeffo, 15 May 2015 - 05:40 PM.


#2 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 15 May 2015 - 05: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.

#3 flaghacker

  • Members
  • 655 posts

Posted 15 May 2015 - 05: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 by flaghacker, 15 May 2015 - 06:03 PM.


#4 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 15 May 2015 - 05:57 PM

View Postflaghacker, on 15 May 2015 - 05:54 PM, said:

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

Edited by Quintuple Agent, 15 May 2015 - 05:58 PM.


#5 flaghacker

  • Members
  • 655 posts

Posted 15 May 2015 - 06:02 PM

View PostQuintuple Agent, on 15 May 2015 - 05:57 PM, said:

View Postflaghacker, on 15 May 2015 - 05:54 PM, said:

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

Thanks, bookmarked that url.

#6 HDeffo

  • Members
  • 214 posts

Posted 15 May 2015 - 06:19 PM

View Postflaghacker, on 15 May 2015 - 05:54 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users