Domanda

I need to export some enums from c++ code. https://github.com/horde3d/Horde3D/blob/master/Horde3D/Bindings/C%2B%2B/Horde3D.h

struct H3DGeoRes
{
   enum List
   {
      GeometryElem = 200,
      //...
   };
};

struct H3DAnimRes
{
   enum List
   {
      EntityElem = 300,
      //...
   };
};

How I can write this in Haskell? Can I do override fromEnum for type?

data H3DGeoRes = GeometryElem | ... deriving (Show, Eq, Ord, Bounded, Enum)
data H3DAnimRes = EntityElem | ... deriving (Show, Eq, Ord, Bounded, Enum)

-- not work

instance Enum H3DGeoRes where
  fromEnum x = (fromEnum x) + 200

instance Enum H3DAnimRes where
  fromEnum x = (fromEnum x) + 300
È stato utile?

Soluzione

One way to solve your problem without too much typing is to create a new type class similar to Enum. Let's call it Enumerable:

class Enumerable a where fromEnumerable :: a -> Int

You can then write the instance for Enumerable using Enum:

instance Enumerable H3DGeoRes where fromEnumerable x = fromEnum x + 200

Whenever using the enumeration of your types H3DGeoRes and H3DAnimRes you will have to use the functions from the Enumerable class and not the Enum class. This is a little bit of a nuisance as it will prevent you from using the convenient list syntax for Enum.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top