batavia icon indicating copy to clipboard operation
batavia copied to clipboard

Callables do not enforce required parameters

Open miki725 opened this issue 9 years ago • 2 comments

For example:

def foo(a):
    print(a)
foo()

results in (traceback from Chrome):

Traceback (most recent call last):
  File "/var/folders/jy/_hlv4sxd78nd0dlxq4p4zmxr0000gn/T/tmpbu2tr58w", line 3, in <module>
  File "/var/folders/jy/_hlv4sxd78nd0dlxq4p4zmxr0000gn/T/tmpbu2tr58w", line 2, in foo
NameError: local variable 'a' referenced before assignment

whereas correct Py behavior is:

>>> def foo(a):
...     print(a)
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() missing 1 required positional argument: 'a'

miki725 avatar Jun 02 '16 23:06 miki725

I'd like to give this one a try.

I guess this could be done this way:

  • functions should be compiled as JS closures containing a list of *argnames
  • on call, assert js!arguments.length == len(argnames)
  • if it fails because missing arguments, use the actual *argnames tuple to index/slice the missing required arguments, produce the correct error.
  • if it fails because too many arguments, complain that "$functionname takes exactly n arguments (m>n given)"

candeira avatar Aug 15 '16 01:08 candeira

@tysonclugg started on this one yesterday. I'm de-dibbing it.

candeira avatar Aug 17 '16 00:08 candeira