{-# OPTIONS -fglasgow-exts #-}

  ( C ) 2004 - 2005 , Oleg Kiselyov &  
   's overlooked object system

module Polymorph where

import Subtype
import Shape
import Circle
import Rectangle


-- We are going to build lists of drawables.

data Drawable = forall a. (Draw a, Start a)
  => Drawable a


  The main function , very much like  's .

main =
      do
         -- handle the shapes polymorphically
         drawloop scribble

         -- handle rectangle specific instance
         arectangle' <- setWidth 30 arectangle
         draw arectangle'

      where
         -- create some shape instances (using existential wrapper)
         scribble = [
            Drawable (Rectangle (Shape 10 20) 5 6),
            Drawable (Circle (Shape 15 25) 8)]

         -- create a rectangle instance
         arectangle = (Rectangle (Shape 0 0) 15 15)


-- Iterate through the list of shapes and draw
drawloop [] = return True
drawloop (Drawable x:xs) =
      do
         draw x
         x' <- rMoveTo 100 100 x
         draw x'
         drawloop xs

# OPTIONS -fglasgow-exts #
 We are going to build lists of drawables.
 handle the shapes polymorphically
 handle rectangle specific instance
 create some shape instances (using existential wrapper)
 create a rectangle instance
 Iterate through the list of shapes and draw

  ( C ) 2004 - 2005 , Oleg Kiselyov &  
   's overlooked object system

module Polymorph where

import Subtype
import Shape
import Circle
import Rectangle



data Drawable = forall a. (Draw a, Start a)
  => Drawable a


  The main function , very much like  's .

main =
      do
         drawloop scribble

         arectangle' <- setWidth 30 arectangle
         draw arectangle'

      where
         scribble = [
            Drawable (Rectangle (Shape 10 20) 5 6),
            Drawable (Circle (Shape 15 25) 8)]

         arectangle = (Rectangle (Shape 0 0) 15 15)


drawloop [] = return True
drawloop (Drawable x:xs) =
      do
         draw x
         x' <- rMoveTo 100 100 x
         draw x'
         drawloop xs
