ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
803 stars 128 forks source link

basics of operator overloading #373

Closed gewang closed 11 months ago

gewang commented 11 months ago

basic operator overloading for 50 operators, binary (e.g., => or +), unary-prefix (e.g., !), and unary-postfix (e.g., ++). The general form for overload an operator is as follows:

// let's say we define a custom class...
public class Foo { int num; }

// persistent operator overloading (trascends contexts)
// NOTE the use of 'public' instead of 'fun' -- it's fun for all!
public Foo @operator =^( Foo lhs, Foo rhs )
{ /* do stuff for Foo =^ Foo */ return rhs; }

// LOCAL binary operator overload for '=>' (local to this context)
fun Foo @operator =>( Foo lhs, Foo rhs )
{ /* do stuff for Foo => Foo */ return rhs; }

// define binary operator overload for '+'
fun Foo @operator +( Foo lhs, Foo rhs )
{ Foo retval; lhs.num + rhs.num => retval.num; return retval; }

// define binary operator overload for '*'
fun Foo @operator *( Foo lhs, Foo rhs )
{ Foo retval; lhs.num * rhs.num => retval.num; return retval; }

// define unary operator overload for '!'
fun int @operator !( Foo foo )
{ return !foo.num; }

// define postfix operator overload for '++'
fun Foo @operator ( Foo foo ) ++
{ foo.num++; return foo; }

using the new overloaded operators:

// create 2 Foos
Foo a, b; 1 => a.num; 2 => b.num;
// operator in action (follows default ck operator precedence)
a + b * b + a @=> Foo c;
// post ++ on c
c++;
// should print 7
<<< c.num >>>;