mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-26 07:22:21 +00:00
Improve operator overload typings (#480)
This commit is contained in:
parent
951b6f9f45
commit
2c365ada5b
14
std/assembly/index.d.ts
vendored
14
std/assembly/index.d.ts
vendored
@ -931,7 +931,10 @@ declare function global(
|
|||||||
): TypedPropertyDescriptor<any> | void;
|
): TypedPropertyDescriptor<any> | void;
|
||||||
|
|
||||||
/** Annotates a method as a binary operator overload for the specified `token`. */
|
/** Annotates a method as a binary operator overload for the specified `token`. */
|
||||||
declare function operator(token: string): (
|
declare function operator(token:
|
||||||
|
"[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" |
|
||||||
|
">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%"
|
||||||
|
): (
|
||||||
target: any,
|
target: any,
|
||||||
propertyKey: string,
|
propertyKey: string,
|
||||||
descriptor: TypedPropertyDescriptor<any>
|
descriptor: TypedPropertyDescriptor<any>
|
||||||
@ -939,19 +942,22 @@ declare function operator(token: string): (
|
|||||||
|
|
||||||
declare namespace operator {
|
declare namespace operator {
|
||||||
/** Annotates a method as a binary operator overload for the specified `token`. */
|
/** Annotates a method as a binary operator overload for the specified `token`. */
|
||||||
export function binary(token: string): (
|
export function binary(token:
|
||||||
|
"[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" |
|
||||||
|
">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%"
|
||||||
|
): (
|
||||||
target: any,
|
target: any,
|
||||||
propertyKey: string,
|
propertyKey: string,
|
||||||
descriptor: TypedPropertyDescriptor<any>
|
descriptor: TypedPropertyDescriptor<any>
|
||||||
) => TypedPropertyDescriptor<any> | void;
|
) => TypedPropertyDescriptor<any> | void;
|
||||||
/** Annotates a method as an unary prefix operator overload for the specified `token`. */
|
/** Annotates a method as an unary prefix operator overload for the specified `token`. */
|
||||||
export function prefix(token: string): (
|
export function prefix(token: "!" | "~" | "+" | "-" | "++" | "--"): (
|
||||||
target: any,
|
target: any,
|
||||||
propertyKey: string,
|
propertyKey: string,
|
||||||
descriptor: TypedPropertyDescriptor<any>
|
descriptor: TypedPropertyDescriptor<any>
|
||||||
) => TypedPropertyDescriptor<any> | void;
|
) => TypedPropertyDescriptor<any> | void;
|
||||||
/** Annotates a method as an unary postfix operator overload for the specified `token`. */
|
/** Annotates a method as an unary postfix operator overload for the specified `token`. */
|
||||||
export function postfix(token: string): (
|
export function postfix(token: "++" | "--"): (
|
||||||
target: any,
|
target: any,
|
||||||
propertyKey: string,
|
propertyKey: string,
|
||||||
descriptor: TypedPropertyDescriptor<any>
|
descriptor: TypedPropertyDescriptor<any>
|
||||||
|
@ -4,137 +4,137 @@ class Tester {
|
|||||||
constructor(public x: i32, public y: i32) {
|
constructor(public x: i32, public y: i32) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('+')
|
@operator("+")
|
||||||
static add(a: Tester, b: Tester): Tester {
|
static add(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x + b.x, a.y + b.y);
|
return new Tester(a.x + b.x, a.y + b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('-')
|
@operator("-")
|
||||||
static sub(a: Tester, b: Tester): Tester {
|
static sub(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x - b.x, a.y - b.y);
|
return new Tester(a.x - b.x, a.y - b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('*')
|
@operator("*")
|
||||||
static mul(a: Tester, b: Tester): Tester {
|
static mul(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x * b.x, a.y * b.y);
|
return new Tester(a.x * b.x, a.y * b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('/')
|
@operator("/")
|
||||||
static div(a: Tester, b: Tester): Tester {
|
static div(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x / b.x, a.y / b.y);
|
return new Tester(a.x / b.x, a.y / b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('%')
|
@operator("%")
|
||||||
static mod(a: Tester, b: Tester): Tester {
|
static mod(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x % b.x, a.y % b.y);
|
return new Tester(a.x % b.x, a.y % b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('**')
|
@operator("**")
|
||||||
static pow(a: Tester, b: Tester): Tester {
|
static pow(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(<i32>(a.x ** b.x), <i32>(a.y ** b.y));
|
return new Tester(<i32>(a.x ** b.x), <i32>(a.y ** b.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('|')
|
@operator("|")
|
||||||
static or(a: Tester, b: Tester): Tester {
|
static or(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x | b.x, a.y | b.y);
|
return new Tester(a.x | b.x, a.y | b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('&')
|
@operator("&")
|
||||||
static and(a: Tester, b: Tester): Tester {
|
static and(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x & b.x, a.y & b.y);
|
return new Tester(a.x & b.x, a.y & b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('^')
|
@operator("^")
|
||||||
static xor(a: Tester, b: Tester): Tester {
|
static xor(a: Tester, b: Tester): Tester {
|
||||||
return new Tester(a.x ^ b.x, a.y ^ b.y);
|
return new Tester(a.x ^ b.x, a.y ^ b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('==')
|
@operator("==")
|
||||||
static equals(a: Tester, b: Tester): bool {
|
static equals(a: Tester, b: Tester): bool {
|
||||||
return a.x == b.x && a.y == b.y;
|
return a.x == b.x && a.y == b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('!=')
|
@operator("!=")
|
||||||
static notEquals(a: Tester, b: Tester): bool {
|
static notEquals(a: Tester, b: Tester): bool {
|
||||||
return a.x != b.x && a.y != b.y;
|
return a.x != b.x && a.y != b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('>')
|
@operator(">")
|
||||||
static greater(a: Tester, b: Tester): bool {
|
static greater(a: Tester, b: Tester): bool {
|
||||||
return a.x > b.x && a.y > b.y;
|
return a.x > b.x && a.y > b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('>=')
|
@operator(">=")
|
||||||
static greaterEquals(a: Tester, b: Tester): bool {
|
static greaterEquals(a: Tester, b: Tester): bool {
|
||||||
return a.x >= b.x && a.y >= b.y;
|
return a.x >= b.x && a.y >= b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('<')
|
@operator("<")
|
||||||
static less(a: Tester, b: Tester): bool {
|
static less(a: Tester, b: Tester): bool {
|
||||||
return a.x < b.x && a.y < b.y;
|
return a.x < b.x && a.y < b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('<=')
|
@operator("<=")
|
||||||
static lessEquals(a: Tester, b: Tester): bool {
|
static lessEquals(a: Tester, b: Tester): bool {
|
||||||
return a.x <= b.x && a.y <= b.y;
|
return a.x <= b.x && a.y <= b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('>>')
|
@operator(">>")
|
||||||
static shr(value: Tester, shift: i32): Tester {
|
static shr(value: Tester, shift: i32): Tester {
|
||||||
return new Tester(value.x >> shift, value.y >> shift);
|
return new Tester(value.x >> shift, value.y >> shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('>>>')
|
@operator(">>>")
|
||||||
static shu(value: Tester, shift: i32): Tester {
|
static shu(value: Tester, shift: i32): Tester {
|
||||||
return new Tester(value.x >>> shift, value.y >>> shift);
|
return new Tester(value.x >>> shift, value.y >>> shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator('<<')
|
@operator("<<")
|
||||||
static shl(value: Tester, shift: i32): Tester {
|
static shl(value: Tester, shift: i32): Tester {
|
||||||
return new Tester(value.x << shift, value.y << shift);
|
return new Tester(value.x << shift, value.y << shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
// unary opterators
|
// unary opterators
|
||||||
@operator.prefix('~')
|
@operator.prefix("~")
|
||||||
static not(value: Tester): Tester {
|
static not(value: Tester): Tester {
|
||||||
return new Tester(~value.x, ~value.y);
|
return new Tester(~value.x, ~value.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.prefix('!')
|
@operator.prefix("!")
|
||||||
static excl(value: Tester): bool {
|
static excl(value: Tester): bool {
|
||||||
return !value.x && !value.y;
|
return !value.x && !value.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.prefix('+')
|
@operator.prefix("+")
|
||||||
static pos(value: Tester): Tester {
|
static pos(value: Tester): Tester {
|
||||||
return new Tester(+value.x, +value.y);
|
return new Tester(+value.x, +value.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.prefix('-')
|
@operator.prefix("-")
|
||||||
static neg(value: Tester): Tester {
|
static neg(value: Tester): Tester {
|
||||||
return new Tester(-value.x, -value.y);
|
return new Tester(-value.x, -value.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.prefix('++')
|
@operator.prefix("++")
|
||||||
inc(): this {
|
inc(): this {
|
||||||
++this.x;
|
++this.x;
|
||||||
++this.y;
|
++this.y;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.prefix('--')
|
@operator.prefix("--")
|
||||||
dec(): this {
|
dec(): this {
|
||||||
--this.x;
|
--this.x;
|
||||||
--this.y;
|
--this.y;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.postfix('++')
|
@operator.postfix("++")
|
||||||
postInc(): Tester {
|
postInc(): Tester {
|
||||||
return new Tester(this.x + 1, this.y + 1);
|
return new Tester(this.x + 1, this.y + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@operator.postfix('--')
|
@operator.postfix("--")
|
||||||
postDec(): Tester {
|
postDec(): Tester {
|
||||||
return new Tester(this.x - 1, this.y - 1);
|
return new Tester(this.x - 1, this.y - 1);
|
||||||
}
|
}
|
||||||
@ -297,12 +297,12 @@ assert(incdec.x == 0 && incdec.y == 1);
|
|||||||
class TesterInlineStatic {
|
class TesterInlineStatic {
|
||||||
constructor(public x: i32, public y: i32) {
|
constructor(public x: i32, public y: i32) {
|
||||||
}
|
}
|
||||||
@inline @operator('+')
|
@inline @operator("+")
|
||||||
static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic {
|
static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic {
|
||||||
return new TesterInlineStatic(a.x + b.x, a.y + b.y);
|
return new TesterInlineStatic(a.x + b.x, a.y + b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@inline @operator.postfix('++')
|
@inline @operator.postfix("++")
|
||||||
static postInc(a: TesterInlineStatic): TesterInlineStatic {
|
static postInc(a: TesterInlineStatic): TesterInlineStatic {
|
||||||
return new TesterInlineStatic(a.x + 1, a.y + 1);
|
return new TesterInlineStatic(a.x + 1, a.y + 1);
|
||||||
}
|
}
|
||||||
@ -317,12 +317,12 @@ assert(ais.x == 4 && ais.y == 6);
|
|||||||
class TesterInlineInstance {
|
class TesterInlineInstance {
|
||||||
constructor(public x: i32, public y: i32) {
|
constructor(public x: i32, public y: i32) {
|
||||||
}
|
}
|
||||||
@inline @operator('+')
|
@inline @operator("+")
|
||||||
add(b: TesterInlineInstance): TesterInlineInstance {
|
add(b: TesterInlineInstance): TesterInlineInstance {
|
||||||
return new TesterInlineInstance(this.x + b.x, this.y + b.y);
|
return new TesterInlineInstance(this.x + b.x, this.y + b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@inline @operator.postfix('++')
|
@inline @operator.postfix("++")
|
||||||
postInc(): TesterInlineInstance {
|
postInc(): TesterInlineInstance {
|
||||||
return new TesterInlineInstance(this.x + 1, this.y + 1);
|
return new TesterInlineInstance(this.x + 1, this.y + 1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user