[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Need help with macro
From: |
Alex Vong |
Subject: |
Re: Need help with macro |
Date: |
Thu, 13 Dec 2018 09:38:14 +0800 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) |
Hello Mike,
Mike Gran <address@hidden> writes:
> Hey all,
>
> I need help making a macro.
>
> I have an existing procedure of the form
>
> (call-method self method (...))
>
> Note that SELF is a struct, METHOD is a string, and the ellipses can
> be anything.
>
> I would like to make a macro that transforms into the above, but, is
> of the form
>
> (send self (method ...))
>
> where SELF is the same struct, METHOD is a literal symbol, and the
> ellipses are unchanged.
>
> For example, I would like to call
>
> (send date (get-year))
> (send date (set-year 2018))
> (send date (set-dmy 1 1 2018))
>
> and have it be
>
> (call-method date "get-year" '())
> (call-method date "set-year" (list 2018))
> (call-method date "set-dmy" (list 1 1 2018))
>
> I get hung up on figuring out how to handle the literal symbol and the
> ellipses.
>
> If you need context, see
> https://wiki.gnome.org/Projects/GObjectIntrospection/HowToWriteALanguageBinding
>
> Thanks in advance,
>
> Mike Gran
I think this would work:
(define-syntax-rule (send self (method args ...))
(call-method self
(symbol->string 'method)
(list args ...)))
For example,
(send date (set-dmy 1 1 2018))
will be expanded to
(call-method date
(symbol->string 'set-dmy)
(list 1 1 2018))
which is equivalent to
(call-method date "set-dmy" (list 1 1 2018))
Cheers,
Alex
signature.asc
Description: PGP signature