Skip to content

Instantly share code, notes, and snippets.

@rtpg
Created October 1, 2016 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtpg/a95e3d8092008f97a8ff96a9163662b9 to your computer and use it in GitHub Desktop.
Save rtpg/a95e3d8092008f97a8ff96a9163662b9 to your computer and use it in GitHub Desktop.
import Data.Exists (Exists, mkExists, runExists)
type Circle = {x:: Int, y:: Int, r:: Int}
type Rectangle = {x:: Int, y:: Int, w:: Int, h:: Int}
drawCircle :: Circle -> String
drawCircle c = "This is a circle!"
drawRectangle :: Rectangle -> String
drawRectangle r = "This is a rectangle!"
data Component o = Component {
data:: o,
draw:: o -> String
}
aCircle = Component {
data: {x: 100, y:100, r: 100},
draw: drawCircle
}
aRectangle = Component {
data: {x:100, y:100, w:100, h:100},
draw: drawRectangle
}
objects :: Array (Exists Component)
objects = [
mkExists aCircle,
mkExists aRectangle
]
drawObject :: (Exists Component) -> String
drawObject = runExists (\(Component c) -> c.draw c.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment