with macro Tips please ?

1 view
Skip to first unread message

emp...@gmail.com

unread,
Nov 9, 2008, 2:18:08 AM11/9/08
to Nemerle Forum
Hello since there are no initializers in nemerle as C# I want to
implement a with macro uses as below:

def x = newObject("Hello World") with { Foo = "Bar" , Bar = "Foo" } ?


will initialize x's properties as above?

Any tips on this ?

Andrew Davey

unread,
Nov 9, 2008, 8:14:23 AM11/9/08
to Nemerle Forum
It would need to be either

def x = newObject("Hello World") with { Foo = "Bar" ; Bar = "Foo" }

or

def x = newObject("Hello World") with ( Foo = "Bar" , Bar = "Foo" )

to play well with the supported syntax in Nemerle.

I have made a macro like this before, so it is possible. My
implementation probably sucks, but it worked for me!

// Object initializer syntax
// Foo() with { Bar = 42; Buz = "hello" }
public macro @with (obj, assignmentExprs)
{
try {

def createTemp = <[ def temp = $obj ]>;
def assignments(xs)
{
| <[ $(name : name) = $value ]> :: xs => <[ temp.$
(Splicable.Name(name)) = $value ]> : PExpr :: assignments(xs);
| [] => [<[ temp ]> : PExpr];
| _ => throw Exception("'with' must be given a
Sequence of assignment statements.");
};

def exprs = match (assignmentExprs)
{
| PExpr.Sequence(elements) => elements;
| _ => throw Exception("'with' must be given a
Sequence of assignment statements.");
};

<[ { .. $( createTemp :: assignments(exprs) ) } ]>

} catch {
| ex => Message.Error(ex.Message); <[ ]>
Reply all
Reply to author
Forward
0 new messages