Closed type classes

前端 未结 5 2035
悲哀的现实
悲哀的现实 2021-02-05 08:20

Is it possible to create a typeclass that can no longer admit new members (perhaps by using module boundaries)? I can refuse to export a function necessary for a complete instan

5条回答
  •  情深已故
    2021-02-05 08:36

    When all you are interested in that you have an enumertated set of instances, then this trick might help:

    class (Elem t '[Int, Integer, Bool] ~ True) => Closed t where
    
    type family Elem (t :: k) (ts :: [k]) :: Bool where
      Elem a '[] = False
      Elem a (a ': as) = True
      Elem a (b ': bs) = Elem a bs
    
    instance Closed Int
    instance Closed Integer
    -- instance Closed Float -- ERROR
    

提交回复
热议问题