This repository was archived by the owner on Apr 3, 2025. It is now read-only.
This repository was archived by the owner on Apr 3, 2025. It is now read-only.
Idea: just a more simply solution #65
Open
Description
Idea
if an operator is prefixed or suffixed with a dot, it's interpreted as method invocation:
- In form of
a .op b
, equivalent toa[Symbol.op](b)
- In form of
a op. b
, equivalent tob[Symbol.op](a)
. - In form of
a .op
or.op a
whereop
is an unary operator so thata op
is currently-allowing syntax, equivalent toa[Symbol.op]()
- In any of the forms, if the operand could not provide the corresponding method, let it fail as if the invocation has no method.
Examples
Given | De-sugar To |
---|---|
a .* b |
a[Symbol.asterisk](b, true/* left */) |
a *.b |
b[Symbol.asterisk](a, false/* left */) |
a .=== b |
a[Symbol.strictEquals](b, true/* left */) |
a !==. b |
b[Symbol.notStrictEquals](a, false/* left */) |
!.a |
a[Symbol.exclaimaion]() |
++.a |
a[Symbol.plusPlus](true/* isPrefix */) |
a.-- |
a[Symbol.minusMinus](false/* isPrefix */) |
interface Vec3 {
normalize(): Vec3;
[Symbol.add](other: Vec3): Vec3;
[Symbol.asterisk](other: Vec3 | number): Vec3;
}
function zoom(origin: Vec3, dir: Vec3, length: number) {
return origin .+ (dir.normalize() .* length);
}
console.log((1) .+ 1); // Exception: 1[Symbol.add] is not a function
Props and Cons
Props
- Almost zero-overhead, no runtime check.
- Static, can be type-checked.
- Does not affect current codebases.
Cons
- Let me know.
Metadata
Metadata
Assignees
Labels
No labels