jonsterling/Foam — Issue #15: Unified Operator Overloading
Author: jonsterling
Created: 2012-01-06 00:41:04+00:00
State: open

Perhaps have operators compile down to invocations of overloaded C++ functions provided in a standard library. This would allow all operators to be overloaded on objects as well as primitives.

~~~~cpp
namespace foam {
// a template for primitives
template
inline auto plus(const L& l, const R& r) -> decltype(l + r) {
return l + r;
}

// for objects
inline id plus(id l, id r) {
return [l plus:r];
}
~~~~

Then, the following code would work as expected:

~~~~ruby
str := "dog" + "cat".
num := 1 + 2.
i := %3 + %4.
~~~~

~~~~objc
auto str = plus(@"dog",@"cat");
auto num = plus([NSNumber numberWithInt:1],[NSNumber numberWithInt:2]);
auto i = plus(3,4);
~~~~

Drawbacks: people don't like C++. Fuckem?