Contributed by Scott Anderson
Here's one for Cecil... I think. I couldn't get the compiler to completely build on my system, and the included binary wouldn't fully compile my program. Cris Rathman did compile it, however, and says that it works. He had to add the var declarations in front of the fields.Cecil is basically much like Dylan: a functional polymorphic OO language. Hence, the Cecil example looks much like the Dylan one from a structural standpoint.
A few notes:
-- Cecil "shapes" example
-- 1999-11-04 Scott Anderson
-- 1999-11-10 Actually compiled and run by Cris Rathman
-- base shape object definition
template object shape;
var field x(@:shape):num;
var field y(@:shape):num;
-- function to move shapes. note the first arg is a shape, like Python
or Dylan
method move_to(s@:shape, new_x:num, new_y:num):void
{
s.set_x(new_x);
s.set_y(new_y);
}
-- function to move shapes relative to current position.
method r_move_to(s@:shape, del_x:num, del_y:num):void
{
s.set_x(s.x + del_x);
s.set_y(s.y + del_y);
}
-- define a rectangle as a shape
template object rectangle isa shape;
var field width(@:rectangle):num;
var field height(@:rectangle):num;
-- rectangle constructor
method new_rectangle(xpos:num, ypos:num, w:num, h:num):rectangle
{
concrete object isa rectangle { x := xpos, y := ypos, width := w,
height := h }
}
-- draw a rectangle
method draw(r@:rectangle):void
{
"Drawing rectangle at: (".print;
r.x.print;
",".print;
r.y.print;
"), width: ".print;
r.width.print;
", height: ".print;
r.height.print_line;
}
-- define a circle as a shape
template object circle isa shape;
var field radius(@:circle):num;
-- circle constructor
method new_circle(xpos:num, ypos:num, r:num):circle
{
concrete object isa circle { x := xpos, y := ypos, radius := r }
}
-- draw a circle
method draw(c@:circle):void
{
"Drawing circle at: (".print;
c.x.print;
",".print;
c.y.print;
"), radius: ".print;
c.radius.print_line;
}
method do_example():void
{
-- throw the shapes in a vector
let var scribble := [ new_rectangle(10, 20, 5, 6),
new_circle(15,25,8) ];
-- loop through and display the magic of polymorphism
-- note the block being passed to the do method; shades of Smalltalk
scribble.do(&(s:shape){
s.draw();
s.r_move_to(100, 100);
s.draw();
});
-- call a rectangle-specific function
let var r := new_rectangle(0, 0, 15, 15);
set_width(r, 30);
r.draw();
}
do_example();
|