
Thanks. This and previous email are answers to question I asked. But not
the answer to question I mean.
I'll describe the whole task, as Yves Parès suggested.
I'm trying to convert C++ code to Haskell. I have such hierarchy: class
Object, class Item : Object, class Container : Item. Another one example:
class Unit : Object, class Player : Unit. Each constructor do things like
this:
Object::Object()
{
objectType = TYPEMASK_OBJECT;
// ... lots of code ...
}
Item::Item()
{
objectType |= TYPEMASK_ITEM;
// ...
}
Container::Container(): Item()
{
objectType |= (TYPEMASK_ITEM | TYPEMASK_CONTAINER);
// ...
}
What is objectType? This field is used when a networksend packet is
created. In the packet it is 1 byte of flags, so it is in object hierarchy.
So the question was: what type should objectType field have in Haskell? I
think it must not mimic enum. What the structure have I to use? There is
one more problem - there may be lots of objects, lots of, so memory
efficiency is also suggested.
And side question: what to do with classes? =) Maybe there is simple rule
to convert OO hierarchy to FP.
23 січня 2012 р. 12:15 Malcolm Wallace
2012/1/22 Данило Глинський
What is natural Haskell representation of such enum? enum TypeMask { UNIT, GAMEOBJECT,
CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT };
I don't think that definition makes any sense in C, because UNIT is 0, so UNIT | GAMEOBJECT == GAMEOBJECT == 1
Nevertheless, in Haskell something vaguely similar might be:
data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT
// 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 ... };
import Data.Bits data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT | WORLDOBJECT instance Enum TypeMask where fromEnum UNIT = 0x4 fromEnum GAMEOBJECT = 0x8 fromEnum CREATURE_OR_GAMEOBJECT = fromEnum UNIT .|. fromEnum GAMEOBJECT fromEnum WORLDOBJECT = fromEnum UNIT .|. fromEnum PLAYER .|. fromEnum GAMEOBJECT .|. fromEnum DYNAMICOBJECT .|. fromEnum CORPSE
toEnum 0x4 = UNIT toEnum 0x8 = GAMEOBJECT toEnum _ = error "unspecified enumeration value of type TypeMask"
isCreatureOrGameObject :: Int -> Bool isCreatureOrGameObject x = (x .|. fromEnum CREATURE_OR_GAMEOBJECT) /= 0
isWorldObject :: Int -> Bool isWorldObject x = (x .|. fromEnum WORLDOBJECT) /= 0
-- But fundamentally, this is not an idiomatic Haskell way of doing things. -- The other posts in this thread have shown more Haskell-ish translations.