Fork me on GitHub
#xtdb
<
2023-09-21
>
Martynas Maciulevičius12:09:29

Is there a helper that allows me to use quote and unquote without a macro and syntax quote? I'd like to use Clojure's macro syntax to build queries (e.g. a function that returns a part of a query) but I can only use unquote (`~`) with syntax quote (```) and not regular quote (`'`).

tatut12:09:03

what is the problem you are having with syntax quote? is it that symbols are qualified to the ns

tatut12:09:26

but you usually don’t need macros to build queries, just use the map form and thread that through regular functions

Martynas Maciulevičius12:09:51

I want this:

(defn build-query-part [key-sym]
  `[[data :data/key ~key-sym]])

(build-query-part 'hello)

; I get this:
[[my-ns/data :data/key hello]]

; I want this:
[[data :data/key hello]]

tatut12:09:09

why not?

(defn build-query-part [key-sym]
   [['data :data/key key-sym]])

tatut12:09:40

that gives the result you want

Martynas Maciulevičius12:09:11

Because it's weird when I want to use and and or:

; To get this:
'(and [...])

; I need to type this:
(list 'and '[...])

Martynas Maciulevičius12:09:49

So it would be nicer if I'd be able to use basic quote and unquote instead of syntax quote for and:

'(and [a :a/attr ~hi])
^this form is illegal because unquote is not present inside '.

tatut12:09:11

you can use ~'a inside syntax quote to get unqualified… but that is not pretty either

Martynas Maciulevičius12:09:43

Then it would look like this:

`(~'and [~'a :a/attr ~my-outer-symbol])

alexdavis12:09:17

A few people I know are using this with XTDB https://github.com/brandonbloom/backtick

😯 1
alexdavis12:09:03

possible I'm misunderstanding the issue though

Martynas Maciulevičius12:09:40

I'll try what you said. I think the template macro should be exactly what I want :thinking_face:

Martynas Maciulevičius12:09:42

Nice.

(defn build-query-part [key-sym]
  (backtick/template
   [[data :data/key ~key-sym]]))

(build-query-part 'hi)

[[data :data/key hi]]
And the and:
(defn build-query-part [key-sym key-sym2]
  (backtick/template
   [(and [data :data/key ~key-sym]
         [data :data/key2 ~key-sym2])]))

(build-query-part 'hi 'there)

[(and [data :data/key hi]
      [data :data/key2 there])]