Permalink
Fetching contributors…
Cannot retrieve contributors at this time
37 lines (30 sloc) 764 Bytes
// simple point
// printer is abstract callback method
point printer =
(var x = 0;
var y = 0;
{
show () =
printer "at \(x),\(y)\n",
move x' y' =
x := x';
y := y'
});
// alternative implementation would be to wrap the printer ;)
// coloredPoint adds color accessor and overrides show method to print color
coloredPoint color printer =
(p = point printer;
p with {
color,
show () =
printer "\(color) ";
p.show ()
});
// this "class" provides printer using standard print function
coloredPrintPoint color =
coloredPoint color print;
// and it works...
cp = coloredPrintPoint "red";
cp.move 13 42;
cp.show ();
println "cp is \(cp.color)";