Better Way to Define an Enum in Haskell

前端 未结 5 701
囚心锁ツ
囚心锁ツ 2021-02-01 01:48

I want a datatype to represent a finite set of integers that can be addressed by specific names. I figure the best way to do that is to use an Enum.

However, there is o

5条回答
  •  情书的邮戳
    2021-02-01 01:54

    Since you say the numbers are not generated by any regular law, you could use generic programming (e.g. with Scrap Your Boilerplate) or Template Haskell to implement a generic solution to this problem. I tend to prefer Template Haskell because it actually generates code and compiles it, so you get all the type-checking and optimisation benefits of GHC.

    I wouldn't be surprised if someone had implemented this already. It should be trivial.

提交回复
热议问题