This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-24
Channels
Does clj-kondo work with .cljc file? Given
(defn epoch-ms
"Returns ms from epoch."
[]
(System/currentTimeMillis))
I gotAnd for
(defmethod print-method Duration
[v ^java.io.Writer w]
(.write w (str "#-duration \"" (subs (str v) 2) "\"")))
I have . This only happens for .cljcIn that case you can use: {:cljc {:features #{:clj}}}
^ @UGC0NEP4Y
Maybe this macro is a better solution and just use a .clj
file:
(defmacro if-bb [then else]
(if (System/getProperty "babashka.version") then else))
Given code
(when-not (System/getProperty "babashka.version")
(extend-protocol clojure.core/Inst
Duration
(inst-ms* [v] (.toMillis ^Duration v))
I got the following error:
Type: java.lang.NullPointerException
Location: /Users/pinkfrog/clj/src/dev/clj/time.clj:82:3
Phase: macroexpand
----- Context ------------------------------------------------------------------
78: ZonedDateTime
79: (inst-ms* [v] (.toEpochMilli (.toInstant ^ZonedDateTime v)))))
80:
81: (when-not (System/getProperty "babashka.version")
82: (extend-protocol clojure.core/Inst
^---
83: Duration
84: (inst-ms* [v] (.toMillis ^Duration v))
85: OffsetDateTime
86: (inst-ms* [v] (.toEpochMilli (.toInstant ^OffsetDateTime v)))
87: ZonedDateTime
----- Stack trace --------------------------------------------------------------
clojure.core/deref-future - <built-in>
clojure.core/deref - <built-in>
clojure.core/extend-protocol - <built-in>
Note that I didn’t use the if-bb
macro since I don’t want to create an extra abstraction.
if-bb does work. But I wonder what makes the difference. So in the case of if-bb, isn’t the compiler expanding the extend-protocol
macro?
But why my
1: (when-not (System/getProperty "babashka.version")
82: (extend-protocol clojure.core/Inst
above gets expanded?or another way to put it: you will preserve the reference to the clojure.core/Inst symbol in the generated code, which will then fail
Sorry I haven’t fully caught it. My understanding is, I have the when-not
to guard against expanding the extend-protocol
in bb. But bb
errs still. I wonder what happens. It seems to me extend-protocol
is expanded regardless the when-not
clause.
user=> (when (pos? -12) THIS_DOESNT_EXIST 3)
Could not resolve symbol: THIS_DOESNT_EXIST [at <repl>]
What do you want to convey?I made a simple example.
(defmacro m1
[expr]
(println expr))
(defmacro m2
[v]
nil)
;; prints ((m2 4))
;; so `m2` is NOT expanded when calling m1.
(m1 ((m2 4)))
> Macros don’t expand their arguments but what they return will be expanded
That’s my understanding. And that’s why I am confused why extend-protocol
is expanded. It is any argument to when-not
and due to the condition, the extend-protocol
shall not be entered.
If you write (when false (extend-type ...))
it expands into (if false (extend-type ..))
btw, when we say compiling. The output is a java class file right? And somehow the clojure, through some jvm mechanims, can load and execute that class fiel?