Streaming in a few moments: https://youtu.be/YHkYp0378Uo
Recording: https://youtu.be/7gp4MZdCDQc
If you have time for a newb question in some of your AoC live broadcasts: Why canโt I (map Integer/parseInt coll)
? The error message confuses me too:
CompilerException java.lang.RuntimeException: Unable to find static field: parseInt in class java.lang.Integer
because Integer/parseInt
compiler converts to static field accessor
and there is no such field in Integer
class
Thanks. So the compiler does something else with (Integer/parseInt coll)
, then, right?
right, first of all compiler will try to identify symbol Integer/parseInt
in some context. In case of (Integer/parseInt x)
it will be recognized as method call
Are there some โnativeโ Clojure objects with similar treatment? Iโve been working in cljc
land mostly, and then mostly on the cljs
side of things, so not all that much Java interop. Part of what AoC does for me is to get some more of the JVM experience.
(So, for instance, in cljs (map js/String.fromCharCode [65 66 67])
is perfectly fine. Or (map js/parseInt ["1" "-1"])
, for that matter.)
@pez clojure has a macro called memfn
but hardly anyone I know uses it. So you just gotta do (map #(Integer/parseInt %) ...)
Yes, that is what I do. (The latter one) ๐ . Iโm just trying to understand why I have to.
memfn
also is only for instances, not static calls, but anyway, it's not used much
https://clojure.org/reference/java_interop#_alternative_macro_syntax
also in guide there is a note about using memfn Note it almost always preferable to do this directly now, with syntax like:
Thanks for memfn
!
yeah, consider it dead
Just when I got to know of it it died.
We're just guiding you through the history of Clojure real quick
You should write a We didnโt start the fire with Clojure context. ๐
Something interesting I tried on the repl:
user=> (macroexpand-all '(map Integer/parseInt ["1" "2"]))
(map Integer/parseInt ["1" "2"])
user=> (macroexpand-all '(map #(Integer/parseInt %) ["1" "2"]))
(map (fn* [p1__149#] (. Integer parseInt p1__149#)) ["1" "2"])
user=> (macroexpand-all '(Integer/parseInt "2"))
(. Integer parseInt "2")
The first call didn't expand into anything, whereas the last call did. So in the first one clojure expects it to be a static field which it is not> (map js/String.fromCharCode [65 66 67])
In JS that makes sense because all functions are firstclass, in Java Integer/parseInt doesn't mean anything, you can't just get a function handle/lambda like that. You could get a method object through reflection but it's not trivial.
or to put it differently, if you write Integer/parseInt
in Java without an argument list, then that implies that you are looking for a static variable, not for a method. Clojure continues that distinction, Foo/bar
vs (Foo/bar ...)
hence the error Unable to find static field