
www.Usenet.com
| <-- __Chronological__ --> | <-- __Thread__ --> |
Hi!
I came up with this example trying to explain the difference between
function overloading in C++ and multiple dispatch in Dylan.
Look at the following code, what you'd expect it to produce, and what
it actually does produce:
#include <iostream>
using namespace std;
class bar;
class foo {
public:
virtual foo& operator*(foo&) { cout << "foo*foo\n"; }
virtual bar& operator*(bar&) { cout << "foo*bar\n"; }
};
class bar : public foo {
public:
virtual bar& operator*(foo&) { cout << "bar*foo\n"; }
virtual bar& operator*(bar&) { cout << "bar*bar\n"; }
};
foo& square(foo& x) {
return x * x;
}
main() {
foo x1;
bar x2;
square(x1);
square(x2); // D'oh!
}
Now compare the Dylan version:
module: foo
define class <foo> (<object>) end;
define class <bar> (<foo>) end;
define method \*(o1 :: <foo>, o2 :: <foo>)
format-out("foo*foo\n");
end method;
define method \*(o1 :: <foo>, o2 :: <bar>)
format-out("foo*bar\n");
end method;
define method \*(o1 :: <bar>, o2 :: <foo>)
format-out("bar*foo\n");
end method;
define method \*(o1 :: <bar>, o2 :: <bar>)
format-out("bar*bar\n");
end method;
define method square(x)
x * x;
end method square;
let x1 = make(<foo>);
let x2 = make(<bar>);
square(x1);
square(x2);
Andreas
--
"The Board views the endemic use of PowerPoint briefing slides instead
of technical papers as an illustration of the problematic methods of
technical communication at NASA."
-- Official report on the Columbia shuttle disaster.
| <-- __Chronological__ --> | <-- __Thread__ --> |