Reputation: 9061
I use {:pre [(number? input )]} function to test whether the input is a number; however, this result to an exception when clojure is not sure what type it is. Foe example:
(number? A)
Unable to resolve symbol…
What's the idiomatic way to make sure the input is a number?
Edit: After I read the docs and tried several times, plus the kindly answers in this post, I find that probably a better question is how to handle exception. The purpose is to prevent someone from input something like a (letter a without any punctuation).
Yes, the function (number? some-value) does work well with numbers, strings. But it can't deal with wrong input like this:
A input-without-quotation-marks
Upvotes: 0
Views: 702
Reputation: 13079
Unable to resolve symbol
is a compile-time exception. A symbol that's not defined cannot be tested by a function because the expression will not compile.
Do you really want to test against random symbols that don't resolve to anything or are you just experimenting in the repl? I suspect the latter, but if for some reason you wanted the former you'd need a macro.
Upvotes: 1
Reputation: 20245
Are you sure that it isn't working?
(defn numbers-only [n] {:pre [(number? n)]}
(println "Yay!"))
(numbers-only "clojure")
AssertionError Assert failed: (number? n)
(numbers-only 27)
Yay!
Maybe you are mistyping the name of the param in the pre
condition?
Upvotes: 1