c-enum: To make a type corresponding to an enum of C language

[ bsd3, foreign, library ] [ Propose Tags ]

Please see the README on GitHub at https://github.com/YoshikuniJujo/c-enum#readme


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.1.0, 0.1.1.2, 0.1.1.3
Change log ChangeLog.md
Dependencies base (>=4.7 && <5), template-haskell [details]
License BSD-3-Clause
Copyright 2021 Yoshikuni Jujo
Author Yoshikuni Jujo
Maintainer yoshikuni.jujo.pc@gmail.com
Category Foreign
Home page https://github.com/YoshikuniJujo/c-enum#readme
Bug tracker https://github.com/YoshikuniJujo/c-enum/issues
Source repo head: git clone https://github.com/YoshikuniJujo/c-enum
Uploaded by YoshikuniJujo at 2022-04-08T00:57:35Z
Distributions LTSHaskell:0.1.1.3, NixOS:0.1.1.3, Stackage:0.1.1.3
Reverse Dependencies 5 direct, 2 indirect [details]
Downloads 702 total (28 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-04-08 [all 1 reports]

Readme for c-enum-0.1.1.3

[back to package description]

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