Unary operator overloading example not parsing

2 views
Skip to first unread message

Rodrigo de Salvo Braz

unread,
Mar 15, 2008, 4:50:19 AM3/15/08
to Nemerle Forum
I modified the operator overloading example giving in the "Grokking
the rest" section to:

class Operand {
public val : int;
public this (v : int) { val = v }

public static @<--< (x : Operand) : Operand {
Operand (x.val + 1);
}
}

def x = Operand (2);
def y = <--< x;
assert (y.val == 3);

but it does not parse. Using <--< as a postfix operator does not help.
Am I doing something wrong?

Thanks,

Rodrigo

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

unread,
Mar 16, 2008, 8:07:51 AM3/16/08
to nemer...@googlegroups.com

It's possible that operators beginning with < have a special priority,
which makes them only usable as binary operators... Did you try other
operators and using <--< as binary?

>
> Thanks,
>
> Rodrigo
> >
>

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

Rodrigo de Salvo Braz

unread,
Mar 17, 2008, 6:30:58 AM3/17/08
to Nemerle Forum
On Mar 16, 5:07 am, "Ķaмȋļ ๏ Şκaļşκȋ" <kamil.skal...@gmail.com> wrote:
> On Sat, Mar 15, 2008 at 9:50 AM, Rodrigo de Salvo Braz

> <rodrigob...@gmail.com> wrote:
>
> > I modified the operator overloading example giving in the "Grokking
> > the rest" section to:
>
> > class Operand {
> > public val : int;
> > public this (v : int) { val = v }
>
> > public static @<--< (x : Operand) : Operand {
> > Operand (x.val + 1);
> > }
> > }
>
> > def x = Operand (2);
> > def y = <--< x;
> > assert (y.val == 3);
>
> > but it does not parse. Using <--< as a postfix operator does not help.
> > Am I doing something wrong?
>
> It's possible that operators beginning with < have a special priority,
> which makes them only usable as binary operators... Did you try other
> operators and using <--< as binary?

Yes, it works as binary (the binary case is actually the example on
the wiki for operator overloading).

The problem is getting a unary operator to work. I tried other
symbols, such as !!!! and ++, both prefix and postfix, which the same
results. For the record, the error message I get is

(for the line with def y): error: needed a writable location for
assignment target, got a reference to local symbol `a local value x',
which is read-only
(for the following line): error: there is no member named `val' in
void with type ?

Looks like it is complaining about x not being mutable, but why should
it? I am not modifying it with the operation, I am just computing
something else to assign y with.

If I write 'mutable x' instead of 'def x', I only get the second error
message.

Thanks,

Rodrigo

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

unread,
Mar 17, 2008, 8:14:12 AM3/17/08
to nemer...@googlegroups.com
I don't remember, but it is possible that custom operators (not
predefined in http://nemerle.org/svn/nemerle/trunk/macros/core.n) are
treated as binary by default in parser.
You may add you own specification of how operator should be parsed using
[assembly: Nemerle.Internal.OperatorAttribute ("Nemerle.Core", "<--<",
true, 281, 280)]

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

Reply all
Reply to author
Forward
0 new messages