Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created May 19, 2014 16:58
Show Gist options
  • Select an option

  • Save back2dos/b07b66904d88946281c2 to your computer and use it in GitHub Desktop.

Select an option

Save back2dos/b07b66904d88946281c2 to your computer and use it in GitHub Desktop.
Strict ints for Oleg.
package strict;
import StdTypes.Int in HxInt;
abstract Int(HxInt) from HxInt to HxInt {
@:op(this + b) inline function add(b:Int):Int return this + (b : HxInt);
@:op(this - b) inline function subtract(b:Int):Int return this - (b : HxInt);
@:op(this * b) inline function multiply(b:Int):Int return this * (b : HxInt);
@:op(this >>> b) inline function urshift(b:Int):Int return this >>> (b : HxInt);
@:op(this >> b) inline function rshift(b:Int):Int return this >> (b : HxInt);
@:op(this << b) inline function lshift(b:Int):Int return this << (b : HxInt);
@:op(this & b) inline function and(b:Int):Int return this & (b : HxInt);
@:op(this | b) inline function or(b:Int):Int return this | (b : HxInt);
@:op(this ^ b) inline function xor(b:Int):Int return this ^ (b : HxInt);
@:op(~this) inline function not():Int return ~this;
@:op(-this) inline function neg():Int return this;
@:op(--this) inline function dec():Int return --this;
@:op(++this) inline function inc():Int return ++this;
@:op(this--) inline function rdec():Int return this--;
@:op(this++) inline function rinc():Int return this++;
}
@nadako

nadako commented May 19, 2014

Copy link
Copy Markdown

In recent Haxe I think it's possible even to not specify operator functions when you're abstracting coreType:

abstract Int(HxInt) from HxInt to HxInt {
    @:op(this + b) function add(b:Int):Int;
    @:op(this - b) function subtract(b:Int):Int;
    @:op(this * b) function multiply(b:Int):Int;
    @:op(this >>> b) function urshift(b:Int):Int;
    @:op(this >> b) function rshift(b:Int):Int;
    @:op(this << b) function lshift(b:Int):Int;
    @:op(this & b) function and(b:Int):Int;
    @:op(this | b) function or(b:Int):Int;
    @:op(this ^ b) function xor(b:Int):Int;

    @:op(~this) function not():Int;
    @:op(-this) function neg():Int;

    @:op(--this) function dec():Int;
    @:op(++this) function inc():Int;
    @:op(this--) function rdec():Int;
    @:op(this++) function rinc():Int;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment