Skip to content
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
@stfbd12138

Description

@stfbd12138

Idea

if an operator is prefixed or suffixed with a dot, it's interpreted as method invocation:

  • In form of a .op b, equivalent to a[Symbol.op](b)
  • In form of a op. b, equivalent to b[Symbol.op](a).
  • In form of a .op or .op a where op is an unary operator so that a op is currently-allowing syntax, equivalent to a[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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions