Class level macros

0 views
Skip to first unread message

Andrew Davey

unread,
Aug 5, 2008, 5:07:29 AM8/5/08
to nemerle-dev
Is it possible to create macro that is used within a class definition?

class Address {
public Street : string;
public City : string;
public Zip : string;

validate Street != null && Street.Length > 0;
validate IsValidZipCode(Zip);
}

I know I can call the macro from the top of the class definition
(before the opening brace).
However, being able to use macros in the way I have described would
make for some interesting DSL capabilities; specifically generating
type members.

Ķaмȋļ ๏ Şκaļşκȋ

unread,
Aug 5, 2008, 5:16:33 AM8/5/08
to nemer...@googlegroups.com

Andrew Davey

unread,
Aug 5, 2008, 5:35:05 AM8/5/08
to nemerle-dev
Thanks for the links. It seems like lots of magic is possible.
However, can you give me an example of how to do:

class Foo {
bar; // this is a macro call
}

I'm sorry if I missed an example of that in the nemerle source. They
all seemed to be macros that run on members themselves.
e.g.
bar x : int;


On Aug 5, 10:16 am, "Ķaмȋļ ๏ Şκaļşκȋ" <kamil.skal...@gmail.com> wrote:
> Of course :-)
>
> Seehttp://nemerle.org/svn/nemerle/trunk/macros/assertions.nhttp://nemerle.org/svn/nemerle/trunk/ncc/testsuite/positive/assert.n
>
> orhttp://nemerle.org/svn/nemerle/trunk/macros/concurrency.nhttp://nemerle.org/svn/nemerle/trunk/snippets/concurrency/readerwriter.n
>
> 2008/8/5 Andrew Davey <a.j.da...@gmail.com>:
>
>
>
>
>
>
>
> > Is it possible to create macro that is used within a class definition?
>
> > class Address {
> >   public Street : string;
> >   public City : string;
> >   public Zip : string;
>
> >   validate Street != null && Street.Length > 0;
> >   validate IsValidZipCode(Zip);
> > }
>
> > I know I can call the macro from the top of the class definition
> > (before the opening brace).
> > However, being able to use macros in the way I have described would
> > make for some interesting DSL capabilities; specifically generating
> > type members.
>
> --
> Kamil Skalskihttp://nazgul.omega.pl- Hide quoted text -
>
> - Show quoted text -

Ķaмȋļ ๏ Şκaļşκȋ

unread,
Aug 5, 2008, 5:57:14 AM8/5/08
to nemer...@googlegroups.com
Hm, I think the parser does not support class-level macros to be
placed inside { } brackets. You would need to do
class Foo
bar {

}

(see invariant macro in assertions)

Parser wouldn't know if it should treat your bar as macro for member
or class, so it must be for member. Can't you do something like:

class Foo {
bar
dummy_field : int;
}

?

2008/8/5 Andrew Davey <a.j....@gmail.com>:

--
Kamil Skalski
http://nazgul.omega.pl

Reply all
Reply to author
Forward
0 new messages