Operator Methods

From Adam Colton
Jump to navigation Jump to search

Types can have operator methods. Operators always follow the same precedence and must be immutable.

Operators

  • Add (+Add)
  • Positive (+Positive)
  • Concat (+Concat)
  • Subtract (-Sub)
  • Negate (-Neg)
  • Index ([])
  • logical/bitwise (and, or, not, xor, nor)
  • bit (<< >>)
  • compare (<, =<, ==, !=, >=, >)

One type cannot define both Concat and Add/Positive.

I'm not sure about type enforcement. It would be clean to say that both inputs to an operator must be the same time (what V does). But that doesn't always work, for instance, you can't add two points. So it might make more sense to have Mathematical compliance. So you must have the following consistency

  • A+B=C
  • B=C-A
  • A=C-B

And probably more into multiplication and exponentiation.

For many of these types, function can return two values. For instance on +, the second value is an ok that will return false if the addition overflowed the type.

Limits

It may make sense to limit operator overloading to libraries. The thought being that modules are intended for application (business) logic. These should not be doing complex things.

One counter to this is that we may want to sort, so we may want compare.