Skip to content

Feature Request: + operator should join non-string types if .toString() method exists #2991

@JairusSW

Description

@JairusSW

Feature suggestion

I find it frustrating that both the left and right sides of a String.concat/+ operator cannot join, for example, string and i32.
In JavaScript, it

  1. Checks to see if the right side of the binary expression is a string
  2. If not, it then checks if the right side has a .toString(): string method
  3. If so, it calls it and concatenates left + right.toString()

Would be quite simple to implement. For example, this would probably work:

export class String {
  ...
  @operator("+")
  _concat<T>(left: string, right: T): string {
    if (!isDefined(right.toString())) ERROR(...);
    return this.concat(left, right.toString());
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions