This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-08
Channels
- # bangalore-clj (4)
- # beginners (88)
- # boot (12)
- # cljs-dev (10)
- # cljsjs (1)
- # clojure (284)
- # clojure-denmark (2)
- # clojure-dev (35)
- # clojure-italy (8)
- # clojure-russia (36)
- # clojure-spec (38)
- # clojure-uk (51)
- # clojurescript (145)
- # cursive (6)
- # data-science (1)
- # datomic (8)
- # duct (43)
- # emacs (9)
- # figwheel (2)
- # fulcro (29)
- # graphql (1)
- # immutant (3)
- # instaparse (1)
- # jobs (1)
- # jobs-discuss (1)
- # lumo (16)
- # off-topic (50)
- # onyx (90)
- # re-frame (6)
- # reagent (20)
- # remote-jobs (3)
- # ring-swagger (18)
- # schema (8)
- # shadow-cljs (141)
- # slack-help (3)
- # spacemacs (36)
- # unrepl (7)
- # vim (1)
- # yada (2)
user=> (defn foo ([x]) ([x y]))
#'user/foo
user=> (foo)
ArityException Wrong number of args (0) passed to: user/foo, expected arities: [1, 2] clojure.lang.AFn.throwArity (AFn.java:435)
user=> (defn foo [x])
#'user/foo
user=> (foo)
ArityException Wrong number of args (0) passed to: user/foo, expected arities: [1] clojure.lang.AFn.throwArity (AFn.java:435)
user=> (defn foo [])
#'user/foo
user=> (foo 1)
ArityException Wrong number of args (1) passed to: user/foo, expected arities: [0] clojure.lang.AFn.throwArity (AFn.java:435)
user=>
ie, it informs the user of what arities are expected. Would a ticket/patch for this be interesting?
How does it handle variadic?
public Object throwArity(int n){
Class cls = getClass();
String arities;
try {
java.lang.reflect.Method reqArity = cls.getDeclaredMethod("getRequiredArity");
arities = "[" + reqArity.invoke(this) + "...n]";
} catch (Exception e) {
java.util.Set as = new java.util.HashSet();
java.lang.reflect.Method[] methods = cls.getDeclaredMethods();
for (int i = 0; i < methods.length; i++ ) {
as.add(new Integer(methods[i].getParameterCount()));
}
arities = as.toString();
}
String name = getClass().getSimpleName();
throw new ArityException(n, arities, Compiler.demunge(name));
}
user=> (defn foo ([x y]) ([x y z & rst]))
#'user/foo
user=> (foo)
ArityException Wrong number of args (0) passed to: user/foo, expected arities: [3...n] clojure.lang.AFn.throwArity (AFn.java:442)
user=>
If I were to implement this all on my own, I’d probably choose to add this info to the Function object when it’s being compiled/read/parsed, and then have a function in AFn which returns the available arities.
Because I guess we have all this info at read/analyze time and that we sort of throw it away, and then I try to figure it out again when the exception is to be thrown.
yeah, having it would also fix stuff like https://dev.clojure.org/jira/browse/CLJ-1279
I’m pretty sure it’s not an accident that the expected arities are not an observable part of the function interface
Usually (this use case aside) if you’re asking that question, you’re probably doing something you shouldn’t
But we can (sort of, I guess) ask for the requiredArity for a variadic fn, since
new GeneratorAdapter(ACC_PUBLIC,
Method.getMethod("int getRequiredArity()"),
null,
null,
cv);
But since neither IFn
nor AFn
declares this, it’s still somewhat private. In its own public way.
Anyways, I’d really like to see the accepted arities in the error message, since it has annoyed me quite a few times that it’s not part of it.
And if I were to really wish for something, the line-number of the offending call site would be so much more useful than the fact that the exception is thrown in AFn.java round line 440something.