can i make this to work in squint? the operator-mappings doesn't work i think, i need it for a mongodb dsl query that i made in past
(defn add [& es]
{"$add" (vec es)})
(def operators-mappings
'[+_ add])
(defmacro q [args]
`(let ~operators-mappings
~args))
(prn (q (+_ 2 3)))
;;prints {"$add" [2 3]} in clojure and clojurescriptwhich version of clojurescript did you use before? macros don't just work like that in CLJS unless you use nbb or so
in cljs i put the macros in a .clj file, and in cljs with same name i used require-macros, my version was [org.clojure/clojurescript "1.10.866"]
i am overriding clojure.core symbols thats why want q to be local scope with those, for example this is a mongodb query, and inside it, i use clojure.core/cond for example to refer to the clj one :
(q :people.workers
(< :salary 1000)
(> :years 1)
{:children (filter (fn [:child.] (< :child.age. 15)) :all-children)}
(>= :children 2)
{:bonus (reduce (fn [:total. :child.]
(cond (< :child.age. 5) (+ :total. 100)
(< :child.age. 10) (+ :total. 50)
:else (+ :total. 20)))
0
:children)}
[:!id :name {:new-salary (+ :salary (if- (> :bonus 200) 200 :bonus))}]
(sort :!new-salary))i made mongodb query to be as if we write clojure https://github.com/tkaryadis/squery-mongojs
you need to do a similar thing for squint, you need to use :require-macros but currently you can't use the same filename and the file has to be .cljs or .cljc
you also need to add a squint.edn where you add :paths ["src"] and put the code into a source directory. so e.g.
(:require-macros [takis.macros ...])
and src/takis/macros.cljci read this in documentation, and i tried but i failed, do you know why this code doesn't work?
(defn add [& es]
{"$add" (vec es)})
(def operators-mappings
'[+_ add])
its not macro, i think it doesn't workwhat do you mean with "doesn't work"?
aaah I know
the quote doesn't work in squint, since symbols don't work in squint, they do not exist. but they work in macro namespaces
squint only has JS data structures, no symbols or keywords
keywords are translated to strings
symbols simply don't exist
if you move this to the macro namespace they should work since symbols do exist at compilation time
ok thank you, is there a simple example of using a macro in squint?
how to create one and use it
I'll make an example project, after lunch
thank you, anytime you can : )
i made it working ike it worked in clojurescript, with 2 namespaces : macros.cljc
(ns macros)
(defn add [& es]
"aaa")
(def operators-mappings
'[+_ add])
(defmacro q [args]
`(let ~operators-mappings
~args))
macros.cljs
(ns macros
(:require-macros [macros]))
my normal app namespace
(ns App
(:require [components.ExpenseItem :as ExpenseItem])
(:require-macros [macros :refer [q]]))
(defn add [& es]
"aaa")
(str (q (+_ 1 2)))
seems fine 🙂
indeed! that's how you do it