4 ms·
I'm not sure that this works in more general cases, though. If I have a more general game with more objects of more types in it, adding each type to the render
by AnimalMuppet 13y ago
I'm not sure that this works in more general cases, though.
If I have a more general game with more objects of more types in it, adding each type to the render function is going to get old. In object oriented programming, I'd just call render() on each entry in the list of game objects. But this approach is going to lead me to:
- render each entry in the list of Foo objects
- render each entry in the list of Bar objects
- render each entry in the list of Baz objects
- ...
which doesn't seem to me to work out very well as the game grows more complicated.
(Sorry about all the line breaks - I can't seem to figure out how to get HN to display it right without them.)
- zmoazeni 13y agoThe post links to this page http://www.haskell.org/haskellwiki/Heterogenous_collections http://www.haskell.org/haskellwiki/Heterogenous_collections and one of other solutions is to use existential types to do it. But the author even said: However, I don’t think this is a good use case. We can get around this problem in a cleaner and safer way by using the type system rather than subverting it.
- AnimalMuppet 13y agoRight, I know he said that. But my point is, his solution doesn't scale well to a more complicated problem. (Unless I misunderstood either his solution or your point?)
- tel 13y agoThere are more real solutions which scale better, but in these toy problems it's hard to get to the meat of the problem. Sometimes the "existential antipattern" is a good choice (see Oleg's finally tagless encoding of, say, the linear lambda calculus). Sometimes creative use of static structure can scale much more neatly than lists of concrete objects.
- deleted 13y ago[deleted]
- stonemetal 13y agoThat problem gets especially harry once you consider every subsystem in the game is going to behave similarly, AI, physics, Sound, etc. It seems like the right answer is the same as it is in OO languages favor composition. Grab the Renderable out of the Foos, Bars, and Bazes then pass that list to the render function. It follows the articles suggestion about think about your types more. Why does anyone think the render function needs the entire player state instead of just the part it needs to render something on the screen?
- mkehrt 13y agoSure it does. In object oriented languages, you still have to write all of the same code, you just group it by what data it affects. In (strongly typed) FP, you're writing the same code, but now it's grouped by functionality, rather than by data type.
- gizmo686 13y agoThis approach seems to be what people new to Haskell gennerally go to first (probably because it is the natural solution given the type system). However, as much as we Haskellers hate to admit it, there are design patterns in Haskell that can offer more maintainable solutions that what the language naively presents. In this case, a common pattern is to copy the OO concept of casting. For example, instead of having: class Render a where render :: a -> IO() we could have: class Renderable a where toRender a -> Render along with: data Render = ... render :: Render -> IO() render r = ... This leads to some noise with needing to put a bunch of toRender functions in a short amount of code, but this problem does not get worse as complexity increases.
- andolanra 13y agoMy preferred way to approach this, in Haskell specifically, is to use records as a naïve encoding of objects or interfaces. For example, expanding on the functionality a little bit: data GameEntity = GameEntity { render :: IO () , getPosition :: Point , setPosition :: Point -> GameEntity } makeBall :: Point -> GameEntity makeBall pos = GameEntity { render = myRender , getPosition = pos , setPosition = mySetPos } where myRender = {- draw the ball somehow -} mySetPos newPos = makeBall newPos {- and similar for makePlayer -} That means that the rendering code will look like renderAll :: [GameEntity] -> IO () renderAll allEntities = mapM_ render allEntities main = do pl = makePlayer 1 (0, 5.0) p2 = makePlayer 2 (10.0, 5.0) b = makeBall (5.0, 5.0) {- ... -} renderAll [p1, p2, b] -- type-checks correctly now What I've done is used a record type to encode the interface that it's supposed to expose, while hiding exactly what the particular implementations of the interface are. It's also nicely extensible; if I wanted to add in some other kind of entity—say, a turtle—all I'd need to do is add a function like makeTurtle :: Point -> ShellColor -> TurtleDisposition -> GameEntity and nothing else need be changed.
- zvrba 13y ago> What I've done is used a record type to encode the interface that it's supposed to expose, I love how Haskellers (in general, not you in particular) bash OO and then come up with the exact same technique of simulating OO that is used in C (a struct of function pointers). Something which OO languages provide out of the box (interfaces, virtual methods, etc). Now, let's take this a step further: how would you simulate double-dispatch, or multiple dispatch in Haskell? This is sorely missing from mainstream OO languages.
- andolanra 13y agoI can't speak for the community as a whole, but the Haskellers I know generally "bash" OO with respect to its use of pervasive mutable state and open recursion, and not because the notion of "object" is an inherently wrong one. Those features must be explicitly included in the above model of object (by manually including reference cells and manually invoking a "method" with its own "instance", respectively) whereas they are implicitly included in every object in most commonly-used OO languages. As for multiple dispatch, you could always use multi-param type classes: {-# LANGUAGE MultiParamTypeClasses #-} class Say a b where say :: a -> b -> IO () instance Say Int () where say = putStrLn "Case one" instance Say Int Int where say = putStrLn "Case two" which corresponds roughly to the CLOS snippet (defgeneric say (a b)) (defmethod say ((a integer) (b null)) (format t "Case one")) (defmethod say ((a integer) (b integer)) (format t "Case two")) I do not believe that there is a way to use the record-based model of objects I showed earlier to do the same thing, but perhaps there is some way that is presently eluding me.