Haskell: Instance definitions for type families

后端 未结 2 1987
余生分开走
余生分开走 2021-01-22 10:53

Lets say we have the following code:

class C t where
  g :: t

instance C Int where
  g = 42

Simple. We can also define functions on Int, like

2条回答
  •  余生分开走
    2021-01-22 11:27

    The following file compiles here:

    {-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies #-}
    
    class C a where g :: a
    type family X
    type instance X = Int
    newtype NewX = NewX X deriving Num
    instance C NewX where g = 43
    

提交回复
热议问题