Fork me on GitHub
#clojurescript
<
2018-02-14
>
richiardiandrea02:02:35

is there a way to have this set while calling things in a macro in CLJS (I am doing some crazy things yes šŸ˜„)

richiardiandrea02:02:05

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")))))

richiardiandrea02:02:38

but child-fn does not have this set when I invoke the macro

richiardiandrea02:02:30

the macro expands to:

cljs.user=> (macroexpand '(ep-cloud.logging/child-logger))
(#object[Function]
 (cljs.core/js-obj "namespace" "cljs.user" "level" "debug"))

richiardiandrea02:02:54

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) šŸ˜„

richiardiandrea02:02:53

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")))))

richiardiandrea02:02:11

One problem there is that the vars are not auto named...I can of course improve on..

bendlas11:02:01

hey, is there an easy way, to use custom read-cond features in my cljs build?

dnolen14:02:20

the discussion has to happen at the level of Clojure, but I already know the answer - no way šŸ™‚

dnolen14:02:00

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

jarohen14:02:39

@dnolen: out of interest (not looking to re-open the discussion) why was it rejected?

dnolen14:02:07

short summary, Rich just didnā€™t like the flexibility. Tooling authors also chimed - understanding arbitrary conditionals would be very challenging / intractable.

jarohen14:02:49

ah, yeah, I suppose it would make tooling life pretty difficult if you didn't know what the conditionals meant - cheers šŸ™‚

justinlee16:02:50

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?

darwin16:02:13

@lee.justin.m you can use different sets of source-paths and provide dummy implementations / empty namespaces for production builds

justinlee16:02:20

@darwin interesting. thanks.

darwin16:02:11

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

bhauman20:02:05

I haven't used the straight browser repl in quite some time

bhauman20:02:43

the default localhost:9000/repl page doesn't currently work?

bhauman20:02:45

ok that page is supposed to sit in an iframe, got it

leongrapenthin20:02:25

given I have imported react per npm/closure integration, how can I now subclass react/Component ?

leongrapenthin20:02:54

is there a mechanism in cljs in place for this or do I still need something like react.createClass

justinlee20:02:23

@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)

leongrapenthin20:02:33

@lee.justin.m can you point me to how I would implement a javascript class from within clojurescript?

leongrapenthin20:02:01

your assumption is correct btw. šŸ™‚

justinlee20:02:35

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.

leongrapenthin20:02:25

thank you thats exactly what i was looking for

gfredericks22:02:34

didn't :require-macros become obsolete? did I hallucinate that?

dnolen22:02:55

@gfredericks itā€™s still there

mfikes22:02:57

@gfredericks It still works, but you can avoid it in many situations.

dnolen22:02:16

but to be clear everything bottoms out in the old stuff in the end

gfredericks22:02:23

trying to refer core.async's [go] in this case

dnolen22:02:31

meaning we have sugar

dnolen22:02:53

@gfredericks should work fine if you have the right core.async release

dnolen22:02:16

and lein deps :tree isnā€™t showing you pulling in the wrong one

gfredericks22:02:44

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?

gfredericks22:02:39

(cljs version is 1.9.946, according to *clojurescript-version*)

dnolen22:02:07

@gfredericks (:require [cljs.core.async :refer [go]])

gfredericks22:02:55

it says go doesn't exist, but I have a real old async so I will try upgrading

mfikes22:02:18

And, if you want to be portable with Clojure

(:require [clojure.core.async :refer [go]])

justinlee22:02:11

@mfikes is there a disadvantage to doing it that way?

dnolen22:02:39

@gfredericks yes thatā€™s what I was saying, you have to use new core.async

gfredericks22:02:04

I read that line but not carefully enough, assumed you were talking about cljs versions

mfikes22:02:09

@justinlee Not any disadavantage that I'm aware of.

justinlee22:02:46

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

gfredericks22:02:23

the other thing I couldn't do was refer goog.string/format

gfredericks22:02:33

should that be possible?

mfikes23:02:43

@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

mfikes23:02:31

Or just avoid goog.string/format altogether, as it is not DCE friendly.

gfredericks23:02:49

Replacing it with manual str calls?

mfikes23:02:26

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

gfredericks23:02:26

I need to start porting the jvm format

mfikes23:02:45

Yeah, it is tempting to do that šŸ™‚ I've looked at it and it seems like a non trivial task.

noisesmith23:02:01

does clojure.pprint/cl-format work in cljs?

darwin23:02:22

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

gfredericks23:02:33

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

darwin23:02:53

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

mfikes23:02:01

I think @noisesmith has it right: Invest the time to learn clojure.pprint/cl-format šŸ™‚

darwin23:02:20

Iā€™m pretty sure that clojure.pprint is not DCE friendly

mfikes23:02:55

True, it is probably worse than goog.string/format

darwin23:02:16

ah, sorry I mean cljs.pprint

mfikes23:02:19

Right, you can (require '[clojure.pprint :refer [cl-format]]) and forget that it is actually in a different namespace

mfikes23:02:14

@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 šŸ™‚

justinlee23:02:02

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

gfredericks23:02:49

oh dangit it uses format

gfredericks23:02:01

it's already parsing though so surely that could be refactored out

justinlee23:02:30

I ā¤ļø the name. More packages should adopt that style of naming.

noisesmith23:02:36

you could use any templating library as long as it was small enough, right?

darwin23:02:30

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

justinlee23:02:12

in my case, in clojurescript when iā€™m ā€œprintingā€ stuff itā€™s just debugging so i donā€™t need complex formatters

justinlee23:02:45

this ainā€™t C so I donā€™t need %d

noisesmith23:02:08

so maybe just format but turned into a no-op if the build profile isnā€™t dev

justinlee23:02:57

iā€™m not sure iā€™d ever kill every log statement even in production

justinlee23:02:09

iā€™m not that good šŸ™‚