MikeInnes / Lazy.jl

I was gonna maintain this package, but then I got high
Other
469 stars 54 forks source link

Better error messages in tupleassign #134

Open ForgottenGensym opened 3 years ago

ForgottenGensym commented 3 years ago

Right now, if a user passes the wrong number of arguments, one of two things can happen:

  1. Too few arguments provided: a somewhat confusing BoundsError without context.
  2. Too many arguments provided: the remaining ones will be ignored, which can cause a ton of subtle issues.

This is what I came up with:

  1. In addition to showing the BoundsError, it also gives the error below.
"""
@rec: [tupleassign] Wrong number of arguments passed in tail-recursive calls
    (Tuple8...) = (Tuple7...)
    (a, b, c, d, e, f, g, h) = (1, 2, 3, 4, 5, 6, 7)
"""
  1. I didn't want to break backwards compatibility, because some people might actually use this feature. This will still work, but it will give a (hopefully) helpful warning explaining what is going on and that this is probably a bad idea.
@warn """
@rec: [tupleassign] Extra arguments passed in recursive calls will be ignored
  > `Lazy.@rec` keeps track of arguments in a tuple, and your recursive
  > call passed extra arguments to the function. This will result (after
  > macro expansion) in a snippet like `(arg1, arg2) = (rec1, rec2, rec3)`
  > Julia allows you to do this (`rec3` will be ignored), but in this case
  > it is most likely a mistake.
"""

It took me forever to figure out what was going on when I made this mistake, and so hopefully this can help out.

Note: The warning bit does require the Logging dependency:

import Logging.@warn