[Haskell-beginners] heterogeneous containers and static typing

Ryan Trinkle ryan.trinkle at gmail.com
Tue Nov 5 15:11:08 UTC 2013


Hi Franco,

Have you considered allowing the user of your game engine to supply his or
her own type for the game state?  You could combine this with any state
that the engine itself needs, like so:

data GameState a = GameState EngineState a

Then, your user could make use of 'GameState MyGameState' to hold both his
or her own game's state as well as the engine's state.


Ryan


On Tue, Nov 5, 2013 at 8:33 AM, Franco <franco00 at gmx.com> wrote:

> Hello,
>
>     I am developing a small game engine (nothing fancy or professional, it
> is
> to test the waters of Haskell). You could say it's a very simple DSL: a
> user would import a library and use its tool to make a game.
>
> The problem I encountered is when defining or getting/setting variables
> for an
> instance game. Let me illustrate
>
>     data GameState = GameState [(String, Int)]
>
> This is a very simple and naive gamestate, an association list consisting
> in (variableName, itsValue). This would mean restricting values to "Int"
> only,
> but I can easily enough add any kind of value with:
>
>     data GameState = GameState [(String, Dynamic)]
>
> An example helper function signature would look like
>
>     getBool :: String -> Either MyError Bool
>     data MyError = NotFound String
>                  | TypeMismatch String
>
> The problem with this approach is that I loathe picking up those two kind
> of
> errors (var not found and type mismatch) at runtime. I would like them to
> be
> found at compile time (like "normal" Haskell).
>
> I thought that a possible solution was to declare a datatype with the
> needed
> variables inside:
>
>     data MyGameVar = MyGameVar { health :: Int,
>                                  message :: String }
>     myvars = MyGameVar 1 "Don't forget your umbrella!"
>
> This would find at compile time typos in variable names and type errors at
> compile time, of course. But then I would have to change the GameState to:
>
>     data GameState = GameState MyGameVar
>
> which would be fine if I were developing a *game*, but it is not fine when
> you develop a *game engine* (that would mean having to rewrite MyGameVar
> every game you made).
>
> So I am left to think if the problem I highlighted is "solvable" or do I
> have to make compromises? I guess I am not the first to encounter this
> difficulty, so I am asking here.
> Any hint or idea is appreciated, thanks!
> -F
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131105/8963cd01/attachment.html>


More information about the Beginners mailing list