lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Kenneth Lorber once stated:
> > On Jun 23, 2018, at 8:57 AM, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
> > 
> > Pretty much every function accepts and silently ignores extra arguments,
> > unless the function itself bothers to check for them and throw an
> > explicit error.
> 
> Thanks, and after a little source diving you are correct, but that's not
> quite the reply I was looking for.
> 
> Is there some reason this is a language feature?  Some use-case I don't
> see?  (If so, why is there the occasional check for extra arguments?)

  It's not just Lua.  Here's an example in C:

	----[ x.c ]------
	#include <stdio.h>
	
	extern int foo();
	
	int main(void)
	{
	  int x = foo(1,2,3,4,5);
	  printf("%d\n",x);
	  return 0;
	}


	----[ y.c ]-----
	extern foo(int,int); // to shut up clang

	int foo(int a,int b)
	{
	  return a + b;
	}

[spc]dynamic-147:/tmp>gcc -ansi -pedantic -Wall -Wextra x.c y.c
[spc]dynamic-147:/tmp>clang -ansi -pedantic -Wall -Wextra -Weverything x.c y.c
[spc]dynamic-147:/tmp>./a.out 
3

  No warnings.  No errors.  I'm passing way more parameters to foo() than it
is expecting.  And it works.  Granted, I'm using an outdated style of
function prototype which in C means "this function takes an unspecified
number of arguments and returns an integer" (at least in x.c; y.c has the
proper prototype, but that's to shut up clang about a missing function
prototype that GCC doesn't report).

> Is it documented somewhere I missed?
> 
> Is there any reason not to add checks for extra arguments?  If it's
> performance, it could be available as a compile time option.

  It's performance, and it doesn't hurt.  I'm also not sure how easy it
would be to catch at compile time.

  -spc