Left operators

11 views
Skip to first unread message

Bill Hart

unread,
Sep 17, 2013, 1:48:22 PM9/17/13
to crack-l...@googlegroups.com
Is there a way to define left operators, i.e. suppose you have defined a class C and you want to define a multiplication operator for C which handles the expression

a * c

where a is an int say, and c is of type C?

I can currently only work out how to define right multiplication, and left multiplication does not appear to be rewritten as right multiplication (of course you wouldn't want this for subtraction anyway).

Bill.

Michael Muller

unread,
Sep 17, 2013, 1:50:16 PM9/17/13
to Bill Hart, crack-l...@googlegroups.com

Bill Hart wrote:
> Is there a way to define left operators, i.e. suppose you have defined a
> class C and you want to define a multiplication operator for C which
> handles the expression
>
> a * c
>
> where a is an int say, and c is of type C?

Yes, "oper <binop>" has an "r" modifier:

class X { X oper r+(int i) { return this } }
X x = {}; import crack.io cout; cout `$(100 + x)\n`;

We actually refer to this as "right" multiplication, though, referring to the
location of "this" in the expression (and copying python's __rmul__, __radd__
...)

>
> I can currently only work out how to define right multiplication, and left
> multiplication does not appear to be rewritten as right multiplication (of
> course you wouldn't want this for subtraction anyway).
>
> Bill.
>
> --
> You received this message because you are subscribed to the Google Groups "crack-lang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to crack-lang-de...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>


=============================================================================
michaelMuller = mmu...@enduden.com | http://www.mindhog.net/~mmuller
-----------------------------------------------------------------------------
We are explorers in the further reaches of experience: demons to some, angels
to others. - "Pinhead" from "Hellraiser"
=============================================================================

Bill Hart

unread,
Sep 17, 2013, 2:03:52 PM9/17/13
to crack-l...@googlegroups.com
On 17 September 2013 18:50, Michael Muller <mmu...@enduden.com> wrote:

Bill Hart wrote:
> Is there a way to define left operators, i.e. suppose you have defined a
> class C and you want to define a multiplication operator for C which
> handles the expression
>
> a * c
>
> where a is an int say, and c is of type C?

Yes, "oper <binop>" has an "r" modifier:

  class X { X oper r+(int i) { return this } }
  X x = {}; import crack.io cout;  cout `$(100 + x)\n`;

Thanks!
 

We actually refer to this as "right" multiplication, though, referring to the
location of "this" in the expression (and copying python's __rmul__, __radd__
...)


I probably do too, but I always get these mixed up.

Bill.

Reply all
Reply to author
Forward
0 new messages