Difficult problem.

macro are syntactic tools. So they are not made to evaluate things at runtime.
You could expand to something that call eval at runtime but it is not
a good idea (It involves the compiler at each call)
If your (rest alist) is known at macro-expansion time, then it can
work but to help, I would need to know the context in which you are
using it.


What you want to do would be done by apply for functions.

However, as far as I know, apply do not work on method calls.
If you look at the problem from the java point of view, it is quite hard to
write some code that call a method with an unkown number of argument
without using reflection.

If you have a fix number of argument, you can do this:

(defmacro call-5
 "Calls an instance method on a given object with a list of params."
 [obj method-name params]
 `(apply (fn [x1# x2# x3# x4# x5#] (. ~obj ~(symbol method-name) x1#
x2# x3# x4# x5#) ) ~params))

You can also write a macro that takes the arity as a parameter and do
that trick.

You can also generate an anonymous function with multiple arity
`(apply
  (fn ([x1#] (. ~obj ~(symbol method-name) x1# ))
       ([x1# x2#] (. ~obj ~(symbol method-name) x1#  x2#))
       ([x1# x2# x3#] (. ~obj ~(symbol method-name) x1#  x2# x3#))
      ... up to enough (20 should do the trick) .... ~params)

However, ti might be easier to explain what you are trying to achieve
in a bigger context to see if there is a simpler path.


Best,

Nicolas.



On Mon, Sep 27, 2010 at 6:52 AM, stefanmuenchow <stefanmuenc...@gmx.de> wrote:
> I am a macro newbie... I want to create a macro that calls a function
> with a given name and a parameter list on a given object. My first
> idea was like this:
>
> (defmacro call
>  "Calls an instance method on a given object with a list of params."
>  [obj method-name params]
>  `(. ~obj ~(symbol method-name) ~...@params))
>
> It works fine, if the param list is a simple list, like "(1 2 3 4)",
> but if params is created from an existing list, like "(rest alist)"
> then it doesn't work. So params has to be evaluated first and then the
> single params has to be expanded. How do I do that? I tried it with a
> let block then I got other errors.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to