[Haskell-cafe] [C][enums][newbie] What is natural Haskell representation of such enum?

Erik de Castro Lopo mle+hs at mega-nerd.com
Mon Jan 23 00:19:43 CET 2012


Данило Глинський wrote:

> What is natural Haskell representation of such enum?
> 
> enum TypeMask
> {
>    UNIT,
>    GAMEOBJECT,
> 
>    CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT
> };


data ObjectType = Unit | GameObject

creatureOrGameObject :: ObjectType -> Bool
creatureOrGameObject Unit = True
creatureOrGameObject GameObject = True
creatureOrGameObject _ = False

> More sophisticated question is: and what data structures must be used when
> converting this naturally one to Haskell?
> 
> // 1-byte flaged enum
> enum TypeMask
> {
>    // ...
>    UNIT        = 0x0004,
>    GAMEOBJECT  = 0x0008,
>    // ...
> 
>    CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT
> WORLDOBJECT = UNIT | PLAYER | GAMEOBJECT | DYNAMICOBJECT | CORPSE
>    // ... even more enum combos ...
> };

Pretty much as above, add all the different single bit masks as
constructors to ObjectType and define functions for the ones
that are a combination of one ore more constructor.

Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/



More information about the Haskell-Cafe mailing list