@hackage c-enum0.1.1.3
To make a type corresponding to an enum of C language
Installation
Dependencies (2)
- base >=4.7 && <5
- template-haskell Show all…
Dependents (12)
@hackage/zlib-core, @hackage/moffy-samples-gtk4-run, @hackage/gpu-vulkan-middle, @hackage/cairo-image, @hackage/moffy-samples-gtk3-run, @hackage/glib-stopgap, Show all…
c-enum
foo.h
#ifndef _FOO_H
#define _FOO_H
typedef enum { FOO_ERROR = - 1, FOO_ZERO, FOO_ONE, FOO_TWO, FOO_THREE } Foo;
#endif
Foo.hsc
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE PatternSynonyms #-}
{-# OPTIONS_GHC -Wall -fno-warn-tabs #-}
module Foo where
import Foreign.C.Enum
#include "foo.h"
enum "Foo" ''#{type Foo} [''Show, ''Read, ''Eq] [
("FooError", #{const FOO_ERROR}),
("FooZero", #{const FOO_ZERO}),
("FooOne", #{const FOO_ONE}),
("FooTwo", #{const FOO_TWO}),
("FooThree", #{const FOO_THREE}) ]
You get patterns FooError, ..., FooThree.
And instance Show Foo and instance Read Foo like the following.
> FooOne
FooOne
> Foo 1
FooOne
> Foo 5
Foo 5
> read "FooTwo" :: Foo
FooTwo
> read "Foo 3" :: Foo
FooThree