Best way to get "name" for class and members?

1 view
Skip to first unread message

Andrew Davey

unread,
Jul 13, 2008, 5:24:23 AM7/13/08
to nemerle-dev
I'm creating macros that need to define types, whose names depend on
input parameters.

What is the best way to convert a PExpr into a name for the class?

I'm after something like this:
macro Foo(typeName)
{
def n = ???

create( <[ decl: public class $n { } ]> );
}

So far I've been doing
def n = (typeName :> PExpr.Ref).name.Id
and then
$(n : dyn)

This seems a bit messy... any ideas?

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

unread,
Jul 13, 2008, 11:09:09 AM7/13/08
to nemer...@googlegroups.com
On Sun, Jul 13, 2008 at 11:24 AM, Andrew Davey <a.j....@gmail.com> wrote:
>
> I'm creating macros that need to define types, whose names depend on
> input parameters.
>
> What is the best way to convert a PExpr into a name for the class?

You can match PExpr using quotation, extract the name and then use it
again in quotation like:
match (typeName) {
<[ $(tname : name) ]> =>
<[ class $(tname : name) { public foo() : int { 1 } } ]>
}

The matching part is equivalent to
when (typeName is PExpr.Ref) {
def tname = (typeName :> PExpr.Ref).name
}

>
> I'm after something like this:
> macro Foo(typeName)
> {
> def n = ???
>
> create( <[ decl: public class $n { } ]> );
> }
>
> So far I've been doing
> def n = (typeName :> PExpr.Ref).name.Id
> and then
> $(n : dyn)
>
> This seems a bit messy... any ideas?
> >
>

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

Reply all
Reply to author
Forward
0 new messages