This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-14
Channels
- # beginners (19)
- # boot (11)
- # cider (59)
- # cljs-dev (292)
- # cljsrn (2)
- # clojure (121)
- # clojure-brasil (19)
- # clojure-canada (2)
- # clojure-france (2)
- # clojure-italy (57)
- # clojure-spec (54)
- # clojure-uk (20)
- # clojurescript (83)
- # core-async (20)
- # cursive (5)
- # datascript (2)
- # datomic (10)
- # duct (25)
- # editors (4)
- # emacs (2)
- # fulcro (5)
- # funcool (1)
- # graphql (2)
- # immutant (8)
- # java (1)
- # jobs (4)
- # jvm (1)
- # keechma (5)
- # luminus (10)
- # off-topic (113)
- # om (36)
- # onyx (11)
- # parinfer (55)
- # pedestal (7)
- # protorepl (28)
- # re-frame (25)
- # reagent (6)
- # ring-swagger (1)
- # shadow-cljs (113)
- # spacemacs (1)
- # specter (23)
- # unrepl (8)
- # yada (8)
is there a way to have this
set while calling things in a macro in CLJS (I am doing some crazy things yes š)
In particular:
(defmacro child-logger []
(let [my-ns (str *ns*)
child-fn (aget ep-cloud.logging/parent-logger "child")]
`(~child-fn (js-obj "namespace" ~my-ns
"level" ~(if ^boolean goog.DEBUG "debug" "info")))))
but child-fn
does not have this
set when I invoke the macro
the macro expands to:
cljs.user=> (macroexpand '(ep-cloud.logging/child-logger))
(#object[Function]
(cljs.core/js-obj "namespace" "cljs.user" "level" "debug"))
I am not a JS expert so I might need .call
or .bind
here...however I am seeking some light-shedding on this (pun intended) š
ok this works:
(defmacro child-logger []
(let [my-ns (str *ns*)
;; parent-logger.child ( ... )
child-fn (aget ep-cloud.logging/parent-logger "child")
;; parent-logger.child.bind ( parent-logger ... )
bound-child-fn (.bind child-fn ep-cloud.logging/parent-logger)]
`(~bound-child-fn (js-obj "namespace" ~my-ns
"level" ~(if ^boolean goog.DEBUG "debug" "info")))))
One problem there is that the vars are not auto named...I can of course improve on..
nope, feature request was declined. https://dev.clojure.org/jira/browse/CLJS-2396
the discussion has to happen at the level of Clojure, but I already know the answer - no way š
that flexibility was under consideration at the very beginning of the conditional reading discussion and it was rejected - Iām not aware of any changes to that decision
@dnolen: out of interest (not looking to re-open the discussion) why was it rejected?
short summary, Rich just didnāt like the flexibility. Tooling authors also chimed - understanding arbitrary conditionals would be very challenging / intractable.
ah, yeah, I suppose it would make tooling life pretty difficult if you didn't know what the conditionals meant - cheers š
so if you canāt do reader conditionals, how do people exclude dependencies from production builds? or do you just have to hope that DCE works?
@lee.justin.m you can use different sets of source-paths and provide dummy implementations / empty namespaces for production builds
btw. some code constructs are not āDCE-friendlyā and they will cause ballooning of your production builds even if you donāt actually use the code: https://github.com/binaryage/cljs-devtools/issues/37
given I have imported react per npm/closure integration, how can I now subclass react/Component ?
is there a mechanism in cljs in place for this or do I still need something like react.createClass
@leongrapenthin you can probably do it using the google closure libraries, but it would be easier to use createClass (which is in a separate package now). (Iām assuming youāve made the conscious decision not to use reagent or one of the other libraries that wraps react in an idiomatic way, but iāll mention it just in case)
@lee.justin.m can you point me to how I would implement a javascript class from within clojurescript?
your assumption is correct btw. š
iāve never done it, but take a look at this https://github.com/tonsky/rum/blob/01c4eee59568334f589b424727597afe219606c4/src/rum/core.cljs#L56
i think thatās whatās going on there. i just remember reading that code, so i could be wrong about what it is doing.
thank you thats exactly what i was looking for
didn't :require-macros
become obsolete? did I hallucinate that?
@gfredericks itās still there
@gfredericks It still works, but you can avoid it in many situations.
trying to refer core.async's [go]
in this case
There is a draft explaining all the sugar: https://github.com/mfikes/clojurescript-site/blob/2fe913433c37c6f12848227369dffc59b04ec77e/content/guides/ns-forms.adoc
@gfredericks should work fine if you have the right core.async release
I'm trying (:require [cljs.core.async.macros :refer [go]])
and it says it can't find the ns; maybe the issue in this case is that there isn't any cljs code in the .macros
namespace?
(cljs version is 1.9.946
, according to *clojurescript-version*
)
@gfredericks (:require [cljs.core.async :refer [go]])
it says go doesn't exist, but I have a real old async so I will try upgrading
And, if you want to be portable with Clojure
(:require [clojure.core.async :refer [go]])
@gfredericks Yes, try 0.4.474
@gfredericks yes thatās what I was saying, you have to use new core.async
oh I'm sorry
I read that line but not carefully enough, assumed you were talking about cljs versions
@justinlee Not any disadavantage that I'm aware of.
just asking because sometimes as a new person i see a cljs version and a clojure version and donāt know which to pick or why
the other thing I couldn't do was refer goog.string/format
should that be possible?
@gfredericks That one is strange enough it is not worth worrying about, but instead just following the example at https://clojurescript.org/reference/google-closure-library#requiring-a-function
Replacing it with manual str calls?
@gfredericks Well, it really depends on what you are using format
for. If just to compose a string, definitely str
. If you need to format a number as hexadecimal, or something else that format
can do, that's where it becomes more difficult to find a quick replacement.
I need to start porting the jvm format
Yeah, it is tempting to do that š I've looked at it and it seems like a non trivial task.
does clojure.pprint/cl-format work in cljs?
ad string/format the problem is dynamism, the format string could possibly contain any formatting feature and that is why DCE must include all related code
That was my conclusion a while back, but I just realized that you could do a partial port and have it still be useful for 99% of cases
one could write a macro, which would analyze static formatting strings and rewrite the code to use specific formatting functions, then DCE could kick in
I think @noisesmith has it right: Invest the time to learn clojure.pprint/cl-format
š
Right, you can (require '[clojure.pprint :refer [cl-format]])
and forget that it is actually in a different namespace
@justinlee One disadvantage of using clojure
aliasing, etc is that it may not be appropriate in library code if you wish that library to be consumable by really old versions of ClojureScript š
is there anything lighter weight out there? iāve almost started to write something super simple that just replaces % signs in a template string with positional arguments, just so I could do (print "thing[%] = %" idx (get thing idx))
without breaking up the template string
this might be easy to port to cljc
https://github.com/gfredericks/like-format-but-with-named-args
oh dangit it uses format
it's already parsing though so surely that could be refactored out
you could use any templating library as long as it was small enough, right?
well, to implement something like this to be DCE friendly, it would have to be a macro which at compile time analyzes the format string and emits āsimpleā direct code, in case of dynamic format strings it should bark or implement dynamic code path which would be probably similar to your impl, but user should get warned that they are ruining DCE at that point
in my case, in clojurescript when iām āprintingā stuff itās just debugging so i donāt need complex formatters
so maybe just format but turned into a no-op if the build profile isnāt dev