{ |one, step, back| }

The Shape Example in Io

Contributed by Steve Dekorte

Code for Io

File: polymorph.io

scribble = List clone

// toss the different shapes into a collection
scribble append(Rectangle clone setX(10) setY(20) setWidth(5) setHeight(6))
scribble append(Circle setX(15) setY(25) setRadius(8))

// Use the shapes polymorphically
scribble foreach(s,
  s draw];
  s rMoveToX(100) rMoveToY(100)
  s draw
)

// call a rectangle specific function
aRectangle = Rectangle clone setX(0) setY(0) setWidth(15) setHeight(15)
aRectangle setWidth(30)
aRectangle draw

File: shape.io

Shape := Object clone do(
  x ::= 0
  y ::= 0
  rMoveToX := method(nx, x = x + nx)
  rMoveToY := method(ny, y = y + ny)

File: rectangle.io

Rectangle := Shape clone do(
  height ::= 0
  width ::= 0
  draw := method(
    writeln("Drawing a Rectangle at:(", x, ",", y, ") width ", width, " height ", height)
  )

File: circle.io

Circle ::= Shape clone do(
  radius ::= 0
  draw := method(
     printf("Drawing a Circle at:(", x, ",", y, "), radius ", radius)
  )