please explain where and why to use var-quote

488 views
Skip to first unread message

limux

unread,
Aug 4, 2010, 3:32:47 AM8/4/10
to Clojure
i very confused var-quote, hope someone explain it more detailed than
the clojure.org's, thks.

Meikel Brandmeyer

unread,
Aug 4, 2010, 3:42:45 AM8/4/10
to Clojure
Hi,

On Aug 4, 9:32 am, limux <liumengji...@gmail.com> wrote:

> i very confused var-quote, hope someone explain it more detailed than
> the clojure.org's, thks.

Global values are stored in so-called Vars. Symbols are used in
program code to link to those Vars, ie. to basically give them a name.
So the addition function is stored in a Var named +. So whenever the
compiler sees the symbol + it looks up the Var + refers to and gets
its value, the actual addition function. Now, if you actually want to
access the Var itself, not the function it holds, you need to somehow
tell the compiler this intent. And this is exactly what #' does. When
you write #'+ in your code, you tell the compiler: "Look. I want the
Var named +, not its contents."

This is useful to extract metadata about the value in the function,
like docstrings, argument lists, type hints, etc.

Hope this helps.

Sincerely
Meikel

limux

unread,
Aug 4, 2010, 11:53:55 AM8/4/10
to Clojure
Thanks for your very very helpful help.

Another question is:

defmacro defmodel [model-name]
`(let [sym-model-name ~(symbol (str "app.model." model-name))]
(do

limux

unread,
Aug 4, 2010, 12:17:40 PM8/4/10
to Clojure
Thanks for your very very helpful help.

I want to do something like rails's activerecord orm,
The following is the primary idea:

(defmacro defmodel [model-name]
`(let [temp# ~(symbol (str "app.model." model-name))]
(do
;; create the namespace according to model name
(ns temp#)
;; define some relevant functions in the ns created
previously.
(defn find [] (prn "hello"))
;; over
))

It doesn't work, any advice?

Limux,
Regards

Nicolas Oury

unread,
Aug 4, 2010, 12:24:53 PM8/4/10
to clo...@googlegroups.com
2010/8/4 limux <liumen...@gmail.com>:

> Thanks for your very very helpful help.
>
>  I want to do something like rails's activerecord orm,
> The following is the primary idea:
>
> (defmacro defmodel [model-name]
>   `(let [temp# ~(symbol (str "app.model." model-name))]
>       (do
>           ;; create the namespace according to model name
>           (ns temp#)
>           ;; define some relevant functions in the ns created
> previously.
>           (defn find [] (prn "hello"))
>           ;; over
>        ))
>

This is not what you want.
On a model named thing it evaluates to :
(let [tempXXX app.model.things]
(ns temp#).....
This is not good.

You likely want
(let [ns-symbol (symbol ....)] ; I have my symbol, I can return a term
`(do
(ns ~ns-symbol)
(defn hello [] ....))))

Is it working better?

You might want to have a look to already existing Rails like framework
in Clojure.
(I don't know well which parto of any is the ActiveRecord part, so I
won't tell any name, but there is a thread about web development
on this maling list that could be a could place to start.)

Reply all
Reply to author
Forward
0 new messages