[Haskell-beginners] heterogeneous containers and static typing
Franco
franco00 at gmx.com
Tue Nov 5 13:33:29 UTC 2013
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
More information about the Beginners
mailing list