I'm trying Haskell's Gloss module, and I found a a pattern of things required to properly display an object: Its position, dimensions, scale and Picture representation. This seemed like a good use case of typeclasses, so I wrote this up:
class Displayable d where toPicture :: d -> Picture getDims :: d -> (Float,Float) getPos :: d -> (Float,Float) getScale :: d -> Float setDims :: (Float,Float) -> d -> d setPos :: (Float,Float) -> d -> d setScale :: Float -> d -> d setDims _ d = d setPos _ d = d setScale _ d = d getDims _ = (0,0) getPos _ = (0,0) getScale _ = 0
I've never used customs typeclasses before though, so I have some questions about their use:
First and foremost, is this situation appropriate for a typeclass?
I noticed that I need a lot of methods for this to work (I'll probably be adding
get/setRotationtoo, so it will grow). Is this typical? Is this a sign that I'm trying to encompass too much?
Should I be defining default definitions like I have? For certain scenarios, I may not need to define a particular characteristic, but would like the object to take advantage of the rest of the class (like needing to rotate a world, or get the dimensions of a "non-physical entity"). I realize though, that if the default definitions aren't accounted for down the road, it could lead to some odd results (like objects that have a 0 scale by default, which may render them invisible).
Are getters/setters the best way to achieve what I'm trying to do (a central interface for manipulating displayable objects)?
(I know that representations of data aren't thought of as objects in Haskell, but given I'm trying to represent something visual, I thought the term object would be appropriate)
Any thoughts would be appreciated.