clojure-dev

thheller 2022-07-26T06:10:56.883099Z

hmm I guess there is something in clojure I have never run into before and don't actually understand what is happening

thheller 2022-07-26T06:11:14.812869Z

(ns test.thing)

(defrecord Thing []
  clojure.lang.IFn
  (invoke [this]
    "thing"))

(def thing
  (Thing.))

(def bar (let [] (thing)))

(def foo (thing))

thheller 2022-07-26T06:12:14.586609Z

what is different with (def foo (thing)) that it ends up failing with

Execution error (AbstractMethodError) at test.thing.Thing/applyTo (server.clj:-1).
Method test/thing/Thing.applyTo(Lclojure/lang/ISeq;)Ljava/lang/Object; is abstract

thheller 2022-07-26T06:12:24.446319Z

why is it trying to apply?

thheller 2022-07-26T06:13:31.234409Z

as soon as its used in a top-level def it just fails. (def {:foo (thing)}) or any other collection also fails. wrapping it in a let magically makes it work?

thheller 2022-07-26T06:14:09.472539Z

I guess I must implement applyTo from IFn but I wonder why?

thheller 2022-07-26T06:16:16.419809Z

(going from defrecord to deftype changes the error message but still complains about applyTo)

2022-07-26T06:42:00.041649Z

For "simple" code (like invoking a function bound to a var) unless aot compiling, the compiler doesn't bother generating bytecode, it runs a minimal interpreter, which invokes funtions using apply

🤯 3
nwjsmith 2022-07-27T16:01:12.801699Z

Thanks so much for the pointers. I haven't spent enough time with tools.analyzer, will start there

nwjsmith 2022-07-26T20:56:31.922809Z

This might be a weird question, but do you have any tips for me? I'd like to have the depth of understanding of the compiler that you do. Is it as simple as "read all of the compiler source" 😅?

nwjsmith 2022-07-26T20:57:21.144249Z

Or is this something you learned while trying to make changes to the compiler?

2022-07-26T20:58:20.047199Z

more of the latter then the former, I have a vague idea of the overall structure and what parts exist in the compiler that guides looking for stuff in it, I wouldn't say I have read it all

2022-07-26T21:09:10.749069Z

some familiarity with tools.analyzer output can help a lot, because tools.analyzer does pretty much the same analysis as the compiler does, but the results are nice bits of clojure data.

2022-07-26T21:09:48.492459Z

similarly the clojurescript compiler is a compiler for a clojure like language written in clojure

2022-07-26T06:43:03.013209Z

Which is why the let version works fine, that doesn't count as simple, so it generates bytecode that calls the invoke method for function invocation

thheller 2022-07-26T06:43:38.202109Z

TIL. thanks