Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. heterogeneous containers and static typing (Franco)
2. Re: heterogeneous containers and static typing (Ryan Trinkle)
----------------------------------------------------------------------
Message: 1
Date: Tue, 5 Nov 2013 14:33:29 +0100
From: Franco <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] heterogeneous containers and static
typing
Message-ID: <20131105133329.GA3489@efikamx>
Content-Type: text/plain; charset=us-ascii
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
------------------------------
Message: 2
Date: Tue, 5 Nov 2013 10:11:08 -0500
From: Ryan Trinkle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] heterogeneous containers and static
typing
Message-ID:
<cahnepizwetky6b93h2ryo1a9v-5n3mayv6zl0x0hosrpq9z...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]> 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
> [email protected]
> 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-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 65, Issue 2
****************************************