You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Binding forms such as let are incompatible with defclass as such forms only return the value of the last form.
E.g.
(defclassexample
[]
(let [a true]
{:background (if a :blue:grey)}
[:h4
{:color (if a :black:white)}]
[:&:hover
{:border [[(px1) :solid:grey]]}]))
;; will only output;; [:&:hover;; {:border [[(px 1) :solid :grey]];; as the first 2 forms are ignored due to the let
The root cause of this is defclass macro return semantics being different to defn / do in that it returns all top level forms, instead of the value of the last form. Its understandable once analyzed that the above let example does what it does in that context, but it is initially surprising behavior for a Clojure(Script) programmer. It certainly caught me out because I was trying to bind a common value with let and it took me awhile to realise why all the styles were not coming through.
I'm not sure how to fix this. Maybe a mitigation such as allowing a single form return style for defclass could work ? E.g.
(defclassexample
[]
[(let ... ])
The text was updated successfully, but these errors were encountered:
Hmm.... I can see now why you would expect that a let form would have the same mechanics as the top-level defclass, but I hadn't considered it before. I suppose when support would also be nice to have here. It may be doable, but my gut is that it will be tricky.
In the meantime, perhaps something like this will work:
(defclassexample []
(let [a true]
[:& {:background (if a :blue:grey)}
[:h4 {:color (if a :black:white)}]
[:&:hover ; etc.
]]))
See #13
This is problematic when used in nested forms. We could perhaps add a
special case so only top-level forms are supported, but that might be
more confusing than it's worth. Another alternative could be some
special syntax that triggers us to unpack binding forms (IE: explicitly
opt-in to multi-return) but that might be a bit too arcane, eg:
```clojure
(defclass stylish []
[:* (let [opacity 0.2] ... )])
```
We could also add special binding form replacements to similarly opt-in,
eg:
```clojure
(defclass stylish []
; the & mimics [:&]
(&let [opacity 0.2] ... ))
```
This is all sugar though, so maybe the real solution is just
documentation?
Binding forms such as
let
are incompatible withdefclass
as such forms only return the value of the last form.E.g.
The root cause of this is
defclass
macro return semantics being different todefn
/do
in that it returns all top level forms, instead of the value of the last form. Its understandable once analyzed that the abovelet
example does what it does in that context, but it is initially surprising behavior for a Clojure(Script) programmer. It certainly caught me out because I was trying to bind a common value withlet
and it took me awhile to realise why all the styles were not coming through.I'm not sure how to fix this. Maybe a mitigation such as allowing a single form return style for
defclass
could work ? E.g.The text was updated successfully, but these errors were encountered: