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