@:forward()abstract Resource(AResource){inline public function new(i:Int = 0, name:String = "unnamed"){this = new AResource();this.amount = i;this.name = name;}@:tofunction toInt():Int{return this.amount;}@:op(A - B)function substract(num:Int):Resource{var newResource = new Resource();newResource.amount = this.amount - num;newResource.name = this.name;newResource.minAmount = this.minAmount;newResource.maxAmount = this.maxAmount;return newResource;}@:op(A - B)static inline function substractRight(num:Int, originalValue:Resource):Resource{var newResource = new Resource();newResource.amount = num - originalValue.amount;newResource.name = originalValue.name;newResource.minAmount = originalValue.minAmount;newResource.maxAmount = originalValue.maxAmount;return newResource;}}class AResource{public var amount(default, set):Int = 0;public var name:String = "Unnamed";public var minAmount(default, set):Int = 0;public var maxAmount(default, set):Int = 999999999;public function new(){}// GETTERS AND SETTERSfunction set_amount(v:Int):Int{if (v < minAmount)return amount = minAmount;if (v > maxAmount)return amount = maxAmount;return amount = v;}function set_minAmount(v:Int):Int{if (v < minAmount)return amount = minAmount;return minAmount = v;}function set_maxAmount(v:Int):Int{if (v > maxAmount)return amount = maxAmount;return maxAmount = v;}}
var r:Resource = new Resource(10, "Silver");trace(r - 5); // 5
var r:Resource = new Resource(10, "Silver");trace(5 - r); // ERROR: Invalid field access : amount
@:tofunction toInt():Int {return this.amount;}
@:fromstatic function fromInt( a:Int ):Resource {return new Resource( a );}@:op(A - B)static function substractLeft(originalValue:Resource, num:Int):Resource {
var newResource = new Resource();
newResource.amount = originalValue.amount - num;
newResource.name = originalValue.name;newResource.minAmount = originalValue.minAmount;newResource.maxAmount = originalValue.maxAmount;return newResource;}
@:op(B - A)
static inline function substractRight(num:Int, originalValue:Resource):Resource {var newResource = new Resource();newResource.amount = num - originalValue.amount;newResource.name = originalValue.name;newResource.minAmount = originalValue.minAmount;newResource.maxAmount = originalValue.maxAmount;return newResource;}