please explain where and why to use var-quote

Showing 1-5 of 5 messages
please explain where and why to use var-quote limux 8/4/10 12:32 AM
i very confused var-quote, hope someone explain it more detailed than
the clojure.org's, thks.
Re: please explain where and why to use var-quote Meikel Brandmeyer (kotarak) 8/4/10 12:42 AM
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
Re: please explain where and why to use var-quote limux 8/4/10 8:53 AM
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
Re: please explain where and why to use var-quote limux 8/4/10 9:17 AM
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
Re: please explain where and why to use var-quote Nicolas Oury 8/4/10 9:24 AM
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.)