Re: User defined operators

Daniel Bonniot <[email protected]> Fri, 15 Jul 2005 01:49:04 +0200
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
> I'm new to Nice I like the language (I think it is far better than Java=
).

Welcome ;-)

> =BFIs it possible to define new (maybe oveloaded) operators at the user=
=20
> level?

Yes, for those operators that are recognized by the parser, that is all=20
logical and arithmetic operators.

> I mean, suppose I have two objects such that I can write:
>=20
> objA.equals(objB.plus(objC));
>=20
> Is it possible to define the operators '+' and '=3D' such that the=20
> following is valid?
>=20
> objA =3D objB + objC;

=3D is special, it always denotes assignment, so it's not possible to ove=
rload=20
it. I'm not sure if it would be a good idea to allow that, as it's nice t=
o=20
have some certitude about what =3D means in any context. But you can over=
load +.=20
Suppose your objects are of class A:

A `+`(A a1, A s2) ... // normal method declaration

All operators are as normal methods, they just have special names, which=20
requires that you use ``. The standard operators are actually defined lik=
e=20
this in nice.lang (although many have a special body that generates bytec=
ode=20
directly).

> What's more, I may want to add an operator '<<' such that I can write:
>=20
> mycol << objA;
>=20
> instead of:
>=20
> mycol.add(objA);

Same thing, you should be able to define a method with name `<<`.

Cheers,

Daniel


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click