[plt-scheme] Structure design, Immutable vs Mutable

From: Henk Boom (lunarc.lists at gmail.com)
Date: Wed Sep 26 13:53:57 EDT 2007

I'm working on a personal video game project where I have bodies which
move around in a two dimensional world. I want to try to do this with
immutable types because it they are something I am not yet quite used
to using (coming from primarily imperative languages) and I'd like to
give it a try.

In general, a body can be updated for a given delta time and some
forces which act upon it. In a mutable world, I would do:

(apply-force! body force)*
(update-body! body)

With immutable structures I see that I could do:

;update-body: body number vect-2d -> body
(update-body body dt (vect-2d+ force*))

The problem is that I might have a subclass of this body which needs
more information on update. For example, if I have a player body which
has an orientation as well, I might need to pass it the direction the
user wants it to face, so that it can turn towards it on the update.
With some mutation I could do something like this:

(set-player-target-direction! the-player some-direction)
(map update-body bodies) ; the-player is in bodies so we don't need to
do anything more

Here I would use polymorphism to deal with the player turning on its
update. How would I deal with these sorts of requirements with
immutable structures?

Another problem I see with subclasses is if I have a body method which
moves a body by some distance.

;move-body: body vect-2d -> body

This would create a new body with a changed position but all the old
fields copied over. However, if I have a player object, I would have
to override this method even though the behaviour for moving player is
not that different, just because it would need to know to return a
player instead of a body (and copy the extra fields over).

I feel like there is some conceptual leap I have not made yet. Are
there ways around these problems, or should I give up and move back to
using mutation for this? Any suggestions would be appreciated.

    Henk Boom

Posted on the users mailing list.