Dylan

Contributed by Chris Rathman

Shape class (shape.dylan)

Module:    shape
Synopsis:  OO Shape Polymorphism
Author:    Chris Rathman
Copyright: (C) 2000, CUS.  Permission granted to distribute freely.
Version:   1.00

define module shape
   use functional-dylan;
   use simple-format;
   export <shape>;
   export getX;
   export getY;
   export setX;
   export setY;
   export moveTo;
   export rMoveTo;
   export draw;
end module shape;

// define the shape class slots
define class <shape> (<object>)
   slot x :: <number>, required-init-keyword: x:;
   slot y :: <number>, required-init-keyword: y:;
end class <shape>;

// accessors for x & y
define method getX(this :: <shape>) => rval :: <number>;
   x(this);
end method getX;
define method getY(this :: <shape>) => rval :: <number>;
   y(this);
end method getY;
define method setX(this :: <shape>, new-x :: <number>) => ();
   x-setter(new-x, this);
end method setX;
define method setY(this :: <shape>, new-y :: <number>) => ();
   y-setter(new-y, this);
end method setY;

// move the x & y coordinates
define method moveTo(this :: <shape>, new-x :: <number>, new-y :: <number>) => ( );
   setX(this, new-x);
   setY(this, new-y);
end method moveTo;
define method rMoveTo(this :: <shape>, delta-x :: <number>, delta-y :: <number>) => ( );
   moveTo(this, getX(this) + delta-x, getY(this) + delta-y);
end method rMoveTo;

// virtual routine - draw the shape
define generic draw(this :: <shape>) => ( );

Rectangle class (rectangle.dylan)

Module:    rectangle
Synopsis:  OO Shape Polymorphism
Author:    Chris Rathman
Copyright: (C) 2000, CUS.  Permission granted to distribute freely.
Version:   1.00

define module rectangle
   use functional-dylan;
   use simple-format;
   use shape;
   export <rectangle>;
   export getWidth;
   export getHeight;
   export setWidth;
   export setHeight;
end module rectangle;

// define the rectangle class slots
define class <rectangle> (<shape>)
   slot width :: <number>, required-init-keyword: width:;
   slot height :: <number>, required-init-keyword: height:;
end class <rectangle>;

// accessors for width & height
define method getWidth(this :: <rectangle>) => rval :: <number>;
   width(this);
end method getWidth;
define method getHeight(this :: <rectangle>) => rval :: <number>;
  height(this);
end method getHeight;
define method setWidth(this :: <rectangle>, new-width :: <number>) => ();
   width-setter(new-width, this);
end method setWidth;
define method setHeight(this :: <rectangle>, new-height :: <number>) => ();
   height-setter(new-height, this);
end method setHeight;

// draw the rectangle
define method draw(this :: <rectangle>) => ();
   format-out("Drawing Rectangle at:(%d,%d), width %d, height %d\n",
      getX(this), getY(this), getWidth(this), getHeight(this));
end method draw;

Circle class (shape.dylan)

Module:    circle
Synopsis:  OO Shape Polymorphism
Author:    Chris Rathman
Copyright: (C) 2000, CUS.  Permission granted to distribute freely.
Version:   1.00

define module circle
   use functional-dylan;
   use simple-format;
   use shape;
   export <circle>;
   export getRadius;
   export setRadius;
end module circle;

// define the circle class slots
define class <circle> (<shape>)
   slot radius :: <number>, required-init-keyword: radius:;
end class <circle>;

// accessors for radius
define method getRadius(this :: <circle>) => rval :: <number>;
   radius(this);
end method getRadius;
define method setRadius(this :: <circle>, new-width :: <number>) => ();
   radius-setter(new-width, this);
end method setRadius;

// draw the circle
define method draw(this :: <circle>) => ();
   format-out("Drawing Circle at:(%d,%d), radius %d\n",
      getX(this), getY(this), getRadius(this));
end method draw;

Try shapes function (polymorph.dylan)

Module:    polymorph
Synopsis:  OO Shape Polymorphism
Author:    Chris Rathman
Copyright: (C) 2000, CUS.  Permission granted to distribute freely.
Version:   1.00

define module polymorph
   use functional-dylan;
   use shape;
   use circle;
   use rectangle;
end module polymorph;

define method main() => ( );
   // set up some shape instances
   let scribble = make(<vector>, of: <shape>, size: 2);
   scribble[0] := make(<rectangle>, x: 10, y: 20, width: 5, height: 6);
   scribble[1] := make(<circle>, x: 15, y: 25, radius: 8);

   // iterate through the array and handle shapes polymorphically
   for (each :: <shape> in scribble)
      draw(each);
      rMoveTo(each, 100, 100);
      draw(each);
   end;

   // access a rectangle specific function
   let arec = make(<rectangle>, x: 0, y: 0, width: 15, height: 15);
   setWidth(arec, 30);
   draw(arec);
end method main;

begin
   main();
end;

Library interface (dylan-user.dylan)

Module:    dylan-user
Synopsis:  OO Shape Polymorphism
Author:    Chris Rathman
Copyright: (C) 2000, CUS.  Permission granted to distribute freely.
Version:   1.00

define library polymorph
   use functional-dylan;
   export polymorph;
end library polymorph;

Project file (polymorph.hdp)

comment: This file is generated, please don't edit
format-version:   2
library: polymorph
files:   dylan-user
   shape
   rectangle
   circle
   polymorph
base-address:  0x63F20000
start-function:   main
major-version: 1
minor-version: 0
library-pack:  0
compilation-mode: loose
target-type:   executable
comment: additional keywords

Output

Drawing Rectangle at:(10,20), width 5, height 6
Drawing Rectangle at:(110,120), width 5, height 6
Drawing Circle at:(15,25), radius 8
Drawing Circle at:(115,125), radius 8
Drawing Rectangle at:(0,0), width 30, height 15

Chris Rathman / Chris.Rathman@tx.rr.com