Arc Forumnew | comments | leaders | submitlogin
It's impossible for optional/destructuring functions to take too many arguments
3 points by fallintothis 2540 days ago | 1 comment
While continuing to experiment with the arity overloading from http://arclanguage.org/item?id=16771, I quickly got out of hand with trying to add more features and error-handling. (If anyone's interested, I could throw up the ugly mess on my bitbucket.)

But after all my effort, it got me thinking. Arc already has a version of arity overloading with optional parameters, so what errors does it see fit to trigger? As it turns out...nothing, really:

  arc> (def without-optparm (x y) (list x y))
  #<procedure: without-optparm>
  arc> (without-optparm)
  Error: "procedure  without-optparm: expects 2 arguments, given 0"
  arc> (without-optparm 1)
  Error: "procedure  without-optparm: expects 2 arguments, given 1: 1"
  arc> (without-optparm 1 2)
  (1 2)
  arc> (without-optparm 1 2 3)
  Error: "procedure  without-optparm: expects 2 arguments, given 3: 1 2 3"

  arc> (def with-optparm (x (o y)) (list x y))
  #<procedure:zz>
  arc> (with-optparm)
  Error: "car: expects argument of type <pair>; given ()"
  arc> (with-optparm 1)
  (1 nil)
  arc> (with-optparm 1 2)
  (1 2)
  arc> (with-optparm 1 2 3)
  (1 2)
  arc> (with-optparm 1 2 3 4)
  (1 2)
  arc> (with-optparm 1 2 3 4 5 6 7 8 9 10)
  (1 2)
Now, a look at ac.scm will tell you as much: ac-complex-fn translates fns with optional or destructuring args into a Scheme lambda that takes a single rest parameter. Which means that we even have this:

  arc> (def with-dsparm (x (y z)) (list x y z))
  #<procedure:zz>
  arc> (with-dsparm 1 '(2 3) 4 5 6)
  (1 2 3)
I'm not really concerned about bugs this can introduce. What I actually find odd is that it's taken me this long to notice. What else would I think "yeah, that should be an error" about, even though I've never actually run into the problem?


1 point by Pauan 2540 days ago | link

"I'm not really concerned about bugs this can introduce. What I actually find odd is that it's taken me this long to notice. What else would I think "yeah, that should be an error" about, even though I've never actually run into the problem?"

Just an FYI: Arc/Nu does throw an error, because it uses Racket's optional-arg support. And even when it needs to fall back to full-blown destructuring, it still throws cryptic errors rather than doing nothing.

-----