This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-27
Channels
- # adventofcode (1)
- # announcements (4)
- # beginners (120)
- # calva (5)
- # cider (12)
- # clara (3)
- # cljdoc (48)
- # cljs-dev (33)
- # cljsrn (4)
- # clojure (124)
- # clojure-dev (43)
- # clojure-europe (2)
- # clojure-italy (168)
- # clojure-nl (2)
- # clojure-spec (7)
- # clojure-uk (79)
- # clojurescript (50)
- # core-logic (6)
- # cursive (12)
- # datascript (1)
- # datomic (8)
- # devcards (2)
- # emacs (5)
- # events (2)
- # figwheel-main (6)
- # fulcro (18)
- # graphql (42)
- # hyperfiddle (3)
- # jobs (1)
- # luminus (2)
- # nrepl (5)
- # off-topic (59)
- # onyx (5)
- # parinfer (2)
- # pathom (10)
- # pedestal (2)
- # portkey (3)
- # re-frame (24)
- # reagent (6)
- # reitit (54)
- # remote-jobs (1)
- # ring (5)
- # shadow-cljs (75)
- # spacemacs (35)
- # sql (22)
- # tools-deps (16)
- # unrepl (10)
Hallo, I'm trying to understand defsc
macro. The defsc
look like this:
(defsc [this props <optional-computed> <optional-css-map-if-using-css>]
{ ...options... }
(dom/div {:onClick (:onClick computed)} (:db/id props)))
The { ...options... }
is just a map right? it's a data. But, why I cant treat { ...options... }
map as clojure data.
I mean the code below doesn't works:
(def my-options { ...options... })
(defsc [this props <optional-computed> <optional-css-map-if-using-css>]
my-options
(dom/div {:onClick (:onClick computed)} (:db/id props)))
@U0C7HNARK defsc is a macro. It does a lot of really nice things behind the scenes including some validations. Guess that is the trade-off with macros in general, it needs literals not variables.
If you're interested in a bit more @U0CKQ19AQ added a two really awesome videos about how he built a macro that extends defsc
source here https://github.com/fulcrologic/fulcro-incubator/blob/develop/src/main/fulcro/incubator/defsc_extensions.clj videos: https://www.youtube.com/watch?v=mXcxTQnwIM4 & https://www.youtube.com/watch?v=0Ityz2SHQOA
@U0C7HNARK Welcome. By "nice validations" I mean compile-time stuff, that's really useful ex: https://gist.github.com/claudiuapetrei/77043c6c18e6861681be59109c4a7b6b
I ended up wite my own macro.
(defmacro defcomponent
[compname params opts & body]
`(prim/defsc ~compname ~params
~(or opts {})
~@body))
It does work.@U0C7HNARK You cannot do that because I prefer error-checking the data over this particular use-case (which isn’t even that useful), and there is no reliable way to expand an expression at compile time.
For example, if you destructure the wrong things (you have not queried for) you’ll get useful error messages instead of runtime bad behavior
Your macro does nothing useful I can see…the defsc
is already ok with an optional opts. Your hack is just making the macro think that you didn’t pass options. It is certainly broken
The macro you wrote is making defsc
think that the code body has an extra or
expression…it is definitely not using them as the proper options.
I see your point. It's hard problem to solve checking expression at compile time.
In my own use case, I have to do something with opts
. Not so much, but little tricky.
And yes opts & body
is just wrong :rolling_on_the_floor_laughing:.
Btw, thank you for the feedback and all the hard work. It's such a nice community.
anything you have to do with opts needs to run in a macro, and emit a literal map, not a symbol or expression