Fork me on GitHub
#lambdaisland
<
2020-12-09
>
plexus06:12:04

Streaming in a few moments: https://youtu.be/YHkYp0378Uo

pez11:12:29

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

delaguardo11:12:52

because Integer/parseInt compiler converts to static field accessor

delaguardo11:12:18

and there is no such field in Integer class

pez12:12:32

Thanks. So the compiler does something else with (Integer/parseInt coll), then, right?

delaguardo12:12:03

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

pez14:12:18

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.

pez14:12:39

(So, for instance, in cljs (map js/String.fromCharCode [65 66 67]) is perfectly fine. Or (map js/parseInt ["1" "-1"]), for that matter.)

borkdude14:12:55

@pez clojure has a macro called memfn but hardly anyone I know uses it. So you just gotta do (map #(Integer/parseInt %) ...)

pez14:12:59

Yes, that is what I do. (The latter one) šŸ˜ƒ . Iā€™m just trying to understand why I have to.

borkdude14:12:02

memfn also is only for instances, not static calls, but anyway, it's not used much

delaguardo14:12:25

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:

šŸ’Æ 3
pez14:12:39

Thanks for memfn !

borkdude14:12:10

yeah, consider it dead

pez14:12:10

Just when I got to know of it it died.

borkdude14:12:13

We're just guiding you through the history of Clojure real quick

šŸ˜† 3
pez14:12:01

You should write a We didnā€™t start the fire with Clojure context. šŸ˜‚

oxalorg (Mitesh)14:12:46

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

šŸ‘ 3
plexus14:12:17

> (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.

plexus14:12:41

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

šŸ’Æ 9
ā¤ļø 3