Pattern match ClassMember.Function problem

0 views
Skip to first unread message

Andrew Davey

unread,
Aug 5, 2008, 7:12:34 AM8/5/08
to nemerle-dev
Why does this work:

def defineInterfaceMember(member : ClassMember) {
| <[ decl: ..$_ $_ (..$parms) : $t ]> as method =>
interfaceDefn.Define(<[ decl:
$(method.header.ParsedName : name) (..$parms) : $t
]>);


But this doesn't:

def defineInterfaceMember(member : ClassMember) {
| <[ decl: ..$_ $(n : name) (..$parms) : $t ]> =>
interfaceDefn.Define(<[ decl:
$(n : name) (..$parms) : $t
]>);

compile error: pattern variable 'n' already seen in this pattern

Andrew Davey

unread,
Aug 5, 2008, 7:15:00 AM8/5/08
to nemerle-dev
Are there any macros in the Nemerle sources that pattern match class
members?
I could do with some inspiration :)

Andrew Davey

unread,
Aug 5, 2008, 7:22:04 AM8/5/08
to nemerle-dev
I found this:

// Example
// <[ decl: ..$attrs $n < ..$tparms> (..$fparms) : $t where ..
$cts
// implements ..$impl $body ]>

which I assume should be:

<[ decl: ..$attrs $n [..$tparms] (..$fparms) : $t where ..$cts
implements [..$impl] $body ]>

But I stil get the same error about "n".
> > compile error: pattern variable 'n' already seen in this pattern- Hide quoted text -
>
> - Show quoted text -

Andrew Davey

unread,
Aug 5, 2008, 1:50:37 PM8/5/08
to nemerle-dev
I want to make an [ExtractInterface] macro that creates an interface
containing any public members of a class.
This is great for people doing test driven development where
interfaces are very important but may change lots during development.
> > - Show quoted text -- Hide quoted text -

Dmitry Ivankov

unread,
Aug 6, 2008, 6:46:37 AM8/6/08
to nemer...@googlegroups.com
I think it's the problem in typer/Macros.n:325+
[Nemerle]

// Example
// <[ decl: ..$attrs $n < ..$tparms> (..$fparms) : $t where ..$cts
// implements ..$impl $body ]>
| ClassMember.Function ( header = Fun_header where ( typarms = Typarms where (tparms, cts),
ret_type = t, parms = fparms),
implemented = implemented, body = bd) =>
def qtparms = quoted_tparms (tparms, cts);
def qhd = make_quoted_funheader (fparms, qtparms, t, qnm);
<[ ClassMember.Function (name = $qnm,
modifiers = $qattrs,
header = $qhd,
implemented = $(lift_with_ellipsis (implemented)),
body = $(quoted_funbody (bd))) ]>
[/Nemerle]

Here qnm goes both to ClassMember.Function.name and ClassMember.Function.header.name.
Don't know which one is needed here, maybe they have to be equal.

And qnm is defined a bit earlier:
[Nemerle]
internal quoted_member (mem : ClassMember) : PExpr {
def qnm = quoted_sstring (mem.name);
[/Nemerle]

You can try to change qnm to PExpr.Wildcard in one of places and see if it works :)

Dmitry Ivankov

unread,
Aug 6, 2008, 8:03:15 AM8/6/08
to nemer...@googlegroups.com
Ok, Wildcard doesn't work at all, have to dig further :)

Dmitry Ivankov

unread,
Aug 7, 2008, 10:31:28 AM8/7/08
to nemer...@googlegroups.com
Here is a working, but not best patch: add one case to patternize_quotation in typing/Macros.n

public patternize_quotation (exp : PExpr) : PExpr
{
  | <[ ClassMember.Function ($name1,
                                           $modifiers,
                                           header = Fun_header ($qtparms, $name2, $qtype, $qparms),
                                           $implemented,
                                           $body) ]> when !(name1 is PExpr.Wildcard) && !(name2 is PExpr.Wildcard) =>
   patternize_quotation (
   <[ ClassMember.Function ($name1,
                                           $modifiers,
                                           header = Fun_header ($qtparms, _, $qtype, $qparms),
                                           $implemented,
                                           $body) ]>)

It just skips one of the names from match pattern.
Looks like they are supposed to be same in your case.
A check that they are really same should be added to patch later :)
Reply all
Reply to author
Forward
0 new messages