Table of Contents for Programming Languages: a survey
Providing binary operators on existing objects
See https://news.ycombinator.com/item?id=23664724 for a discussion of various ways to do this, including:
- implement a method on the left object (must be able to extend the left object)
- have 'right' methods too, and fallback to these if the left object fails to support the operator (Python does this)
- overload operator definitions (C++ does this, and also support the left object way)
- "Rust puts the operators on the left object too, but in some cases you can add methods/trait specializations to left classes. Unfortunately, Rust doesn't let you do this with generics, but that's a longer more complicated topic." [1]
- "the Ruby solution of calling a `coerce` [0] method on the right hand side." [2]
Meta-operators
In Perl6, 'meta-operators' can be combined together to specify an operator (infix function):
eg.
- list of pairs using the zip meta-operator (Z)
- combined with the pair constructor operator (=>) my @a = 'a'..'z' Z=> 0..*; # ( a => 0, b => 1, etc )
- deck of cards using the cross meta-operator (X)
- and the string concatenation operator (~) my @deck = 2..10,<J Q K A> X~ <♡ ♢ ♣ ♠>;
(thanks [3])