1

Imagine in a game, I have to manage the states of several entities. A typical way of managing one state is to:

data WorldState = WorldState {
  temperature :: Double,
  season      :: Season
}



newtype App a = App {
  unApp :: StateT WorldState IO a
} deriving (Functor, Applicative, Monad, MonadState WorldState, MonadIO)

What about when I have to manage several states, ex: I have a character state:

data PersonState = PersonState {
  name :: String,
  age  :: Int,
  clothing :: ClothingStyle
}

Do I have to create a wrapped version with newtype for each state? What if all those states inter-connect with each other? How can I update one state according to the information of other states? For example, I want to update a person's clothingstyle according to the world status of temperature.

Or should I just create one gigantique state that includes all possible states together?

McBear Holden
  • 5,741
  • 7
  • 33
  • 55
  • 3
    Closely related: [*Combining multiple states in StateT*](https://stackoverflow.com/q/13915923/2751851); [*How do I avoid referring to all state variables when updating only a few?*](https://stackoverflow.com/q/40523355/2751851); [*Simulating interacting stateful objects in Haskell*](https://stackoverflow.com/q/39813675/2751851); [*Mix and match stateful computations within the State monad*](https://stackoverflow.com/q/33935835/2751851). – duplode Mar 05 '18 at 18:17
  • To wrap things up, I closed this as a duplicate. I chose as "official" targets [the one which has the best balance between conciseness and comprehensiveness in the answers](https://stackoverflow.com/questions/40523355/how-do-i-avoid-referring-to-all-state-variables-when-updating-only-a-few) and [Davislor's pick](https://stackoverflow.com/questions/39813675/simulating-interacting-stateful-objects-in-haskell). – duplode Mar 06 '18 at 18:01

0 Answers0