Skip to content

[SUGGESTION] Allow use of named operators on non-class types  #430

@orent

Description

@orent

Expression that flow from left to right without evaluation order jumping around are easier to read and write and less error prone. This is probably one of the reasons for the success of the shell pipe. The use of postfix operators in Cpp2 makes it easier to maintain such strict left-to-right order without requiring the use of extraneous parentheses. It also combines particularly well with UFCS.

But this does not work with the infix operators. While compatibility with math notation is important, having an alternative way to perform these operations without triggering the usual operator evaluation precedence rules would be useful.

The most natural alternative spelling for the infix operators is .operator★(). This actually already works in C++ and therefore in Cpp2. It is possible to rewrite g(f(a)+b) as the pure left-to-rightf(a).operator+(b).g() - but it only works when the objects are class types. This does not work with ints, for example.

Suggestion:

  • Make a.operator★(b) always equivalent to ((a) ★ (b))

Possible implementations:

  • Add .operator★() methods to the builtin non-class types
  • Make operator★() global builtin functions that can be used either in the form operator★(a,b) or UFCSed to a.operator★(b)

The latter seems simpler and would just require translating operator★(a,b) to standard template functions cpp2::operator_name(a,b).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions