Can I make a list of types in haskell?

I have different types of data that I defined, and I want to make them all an instance of the class. Is it possible to make a list of types and a map instance above them instead of declaring them all individually?

I mean something like this:

data Type1 = ...
data Type2 = ...

map (instance ClassName) [Type1, Type2]
+4
source share
1 answer

Well actually you can, with something like

{-# LANGUAGE TemplateHaskell #-}

module T where

class C a

data X = X
data Y = Y
data Z = Z

$(fmap concat $ mapM (\t -> [d|instance C $t|]) [[t|X|], [t|Y|], [t|Z|]])

but it amazes me like mass enumeration if you really don't need to be automatically generated (for example, the list of types can change depending on something).

+10
source

Source: https://habr.com/ru/post/1670479/


All Articles