+1, This would have helped me a lot when I first came to Clojure (from
a non-lisp background).


On Oct 25, 11:57 am, MarkSwanson <mark.swanson...@gmail.com> wrote:
> Hello,
>
> Someone recently posed the question: (why doesn't this work)
> (into {} (map #([% (* % %)]) [1 2 3 4]))
>
> (reference:http://groups.google.com/group/clojure/browse_thread/thread/7d3ee57ee...)
>
> The error message was:
> Caused by: java.lang.IllegalArgumentException: Wrong number of args
> passed to: PersistentVector
>
> How many args were being passed anyway? The code looked fine (to me).
> It would have been helpful to me to know the number of args being
> passed was zero.
>
> With the patch below the error message would look like this:
>
> ... java.lang.IllegalArgumentException: Wrong number of args (0)
> passed to: PersistentVector ...
>
> The patch will handle all cases of wrong arity and report the expected
> number of args. In some cases (RestFN) the exception will now report
> the required arity as well as the given arity.
>
> There is one improvement that could still be made to the patch:
> applyTo(ISeq args) -> should report the count of args:
> -   return throwArity();
> +   return throwArity(-1, reqArity); // TODO/BUG: -1 should be count
> of args
>
> I'm running out the door and don't have time to code this.
>
> Please consider this patch - or something like it.
>
> diff --git a/src/jvm/clojure/lang/AFn.java b/src/jvm/clojure/lang/
> AFn.java
> index e2646ad..56afa8f
> 100644
> --- a/src/jvm/clojure/lang/
> AFn.java
> +++ b/src/jvm/clojure/lang/
> AFn.java
> @@ -45,110 +45,110 @@ public void run()
> {
>
>  public Object invoke() throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (0);
>  }
>
>  public Object invoke(Object arg1) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (1);
>  }
>
>  public Object invoke(Object arg1, Object arg2) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (2);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3) throws
> Exception{
> -       return throwArity
> ();
> +       return throwArity
> (3);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (4);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (5);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (6);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7)
>                 throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (7);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (8);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (9);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10) throws
> Exception{
> -       return throwArity
> ();
> +       return throwArity
> (10);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (11);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (12);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object
> arg13)
>                 throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (13);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14)
>                 throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (14);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14,
>                       Object arg15) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (15);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14,
>                       Object arg15, Object arg16) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (16);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14,
>                       Object arg15, Object arg16, Object arg17) throws
> Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (17);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14,
>                       Object arg15, Object arg16, Object arg17, Object
> arg18) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (18);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14,
>                       Object arg15, Object arg16, Object arg17, Object
> arg18, Object arg19) throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (19);
>  }
>
>  public Object invoke(Object arg1, Object arg2, Object arg3, Object
> arg4, Object arg5, Object arg6, Object
> arg7,
>                       Object arg8, Object arg9, Object arg10, Object
> arg11, Object arg12, Object arg13, Object
> arg14,
>                       Object arg15, Object arg16, Object arg17, Object
> arg18, Object arg19, Object
> arg20)
>                 throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (20);
>  }
>
> @@ -157,7 +157,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                       Object arg15, Object arg16, Object arg17, Object
> arg18, Object arg19, Object
> arg20,
>                       Object...
> args)
>                 throws Exception
> {
> -       return throwArity
> ();
> +       return throwArity
> (-1);
>  }
>
>  public Object applyTo(ISeq arglist) throws Exception
> {
> @@ -443,10 +443,35 @@ static public Object applyToHelper(IFn ifn, ISeq
> arglist) throws Exception
> {
>                 }
>  }
>
> -public Object throwArity()
> {
> +public Object throwArity(int argNum)
> {
>         String name = getClass().getSimpleName
> ();
>         int suffix = name.lastIndexOf
> ("__");
> -       throw new IllegalArgumentException("Wrong number of args
> passed to: "
> +       String
> number;
> +       if (argNum == -1)
> {
> +               number =
> "...";
> +       }
> +       else
> {
> +               number = Integer.toString
> (argNum);
> +       }
> +       throw new IllegalArgumentException("Wrong number of args (" +
> number +
> +
> ") passed to:
> "
>                                            + (suffix == -1 ? name :
> name.substring(0, suffix)).replace('_',
> '-'));
>  }
> +
> +public Object throwArity(int argNum, int reqArity)
> {
> +       String name = getClass().getSimpleName
> ();
> +       int suffix = name.lastIndexOf
> ("__");
> +       String
> number;
> +       if (argNum == -1)
> {
> +               number =
> "...";
> +       }
> +       else
> {
> +               number = Integer.toString
> (argNum);
> +       }
> +       throw new IllegalArgumentException("Wrong number of args (" +
> number +
> +
> ") (required=" + reqArity
> +
> +
> ") passed to:
> "
> +                                          + (suffix == -1 ? name :
> name.substring(0, suffix)).replace('_',
> '-'));
> +}
> +
>  }
> diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/
> RT.java
> index a5da74a..c25c489
> 100644
> --- a/src/jvm/clojure/lang/
> RT.java
> +++ b/src/jvm/clojure/lang/
> RT.java
> @@ -476,7 +476,7 @@ static ISeq seqFrom(Object coll)
> {
>         else
> {
>                 Class c = coll.getClass
> ();
>                 Class sc = c.getSuperclass
> ();
> -               throw new IllegalArgumentException("Don't know how to
> create ISeq from: " + c.getSimpleName
> ());
> +               throw new IllegalArgumentException("Don't know how to
> create ISeq from: " + c.getSimpleName() + " / " + c.getName
> ());
>         }
>  }
>
> diff --git a/src/jvm/clojure/lang/RestFn.java b/src/jvm/clojure/lang/
> RestFn.java
> index 4e15f79..d99afef
> 100644
> --- a/src/jvm/clojure/lang/
> RestFn.java
> +++ b/src/jvm/clojure/lang/
> RestFn.java
> @@ -392,7 +392,7 @@ public Object applyTo(ISeq args) throws Exception
> {
>                                         , args.next
> ());
>
>                 }
> -       return throwArity
> ();
> +       return throwArity(-1, reqArity); // TODO/BUG: -1 should be
> count of args
>  }
>
>  public Object invoke() throws Exception
> {
> @@ -401,7 +401,7 @@ public Object invoke() throws Exception
> {
>                 case
> 0:
>                         return doInvoke
> (null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(0,
> reqArity);
>                 }
>
>  }
> @@ -414,7 +414,7 @@ public Object invoke(Object arg1) throws Exception
> {
>                 case
> 1:
>                         return doInvoke(arg1,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(1,
> reqArity);
>                 }
>
>  }
> @@ -429,7 +429,7 @@ public Object invoke(Object arg1, Object arg2)
> throws Exception
> {
>                 case
> 2:
>                         return doInvoke(arg1, arg2,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(2,
> reqArity);
>                 }
>
>  }
> @@ -446,7 +446,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3) throws Exception
> {
>                 case
> 3:
>                         return doInvoke(arg1, arg2, arg3,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(3,
> reqArity);
>                 }
>
>  }
> @@ -465,7 +465,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4)
> throws
>                 case
> 4:
>                         return doInvoke(arg1, arg2, arg3, arg4,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(4,
> reqArity);
>                 }
>
>  }
> @@ -486,7 +486,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 5:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(5,
> reqArity);
>                 }
>
>  }
> @@ -509,7 +509,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 6:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(6,
> reqArity);
>                 }
>
>  }
> @@ -535,7 +535,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 7:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(7,
> reqArity);
>                 }
>
>  }
> @@ -563,7 +563,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 8:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(8,
> reqArity);
>                 }
>
>  }
> @@ -593,7 +593,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 9:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(9,
> reqArity);
>                 }
>
>  }
> @@ -625,7 +625,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 10:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(10,
> reqArity);
>                 }
>
>  }
> @@ -659,7 +659,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 11:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(11,
> reqArity);
>                 }
>
>  }
> @@ -695,7 +695,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 12:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(12,
> reqArity);
>                 }
>
>  }
> @@ -747,7 +747,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                 case
> 13:
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(13,
> reqArity);
>                 }
>
>  }
> @@ -803,7 +803,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
> arg14,
>
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(14,
> reqArity);
>                 }
>
>  }
> @@ -862,7 +862,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
> arg14,
>                                         arg15,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(15,
> reqArity);
>                 }
>
>  }
> @@ -924,7 +924,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4,
> Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13,
> arg14,
>                                         arg15, arg16,
> null);
>
> default:
> -                       return throwArity
> ();
> +                       return throwArity(16,
> reqArity);
>                 }
>
>  }
> @@ -989,7 +989,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4, Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14,
>                                         arg15, arg16, arg17, null);
>                 default:
> -                       return throwArity();
> +                       return throwArity(17, reqArity);
>                 }
>
>  }
> @@ -1058,7 +1058,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4, Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14,
>                                         arg15, arg16, arg17, arg18,
> null);
>                 default:
> -                       return throwArity();
> +                       return throwArity(18, reqArity);
>                 }
>
>  }
> @@ -1133,7 +1133,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4, Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14,
>                                         arg15, arg16, arg17, arg18,
> arg19, null);
>                 default:
> -                       return throwArity();
> +                       return throwArity(19, reqArity);
>                 }
>
>  }
> @@ -1216,7 +1216,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4, Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14,
>                                         arg15, arg16, arg17, arg18,
> arg19, arg20, null);
>                 default:
> -                       return throwArity();
> +                       return throwArity(20, reqArity);
>                 }
>
>  }
> @@ -1307,7 +1307,7 @@ public Object invoke(Object arg1, Object arg2,
> Object arg3, Object arg4, Object
>                         return doInvoke(arg1, arg2, arg3, arg4, arg5,
> arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14,
>                                         arg15, arg16, arg17, arg18,
> arg19, arg20, ArraySeq.create(args));
>                 default:
> -                       return throwArity();
> +                       return throwArity(-1, reqArity);
>                 }
>
>  }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to