Closed type classes

前端 未结 5 2034
悲哀的现实
悲哀的现实 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:39

    Since GHC 7.8.1, closed type families can be declared, and I think with the help of them, and ConstraintKinds, you can do this:

    type family SecretClass (a :: *) :: Constraint where
      SecretClass Int = ()
    

    SecretClass a forms a constraint, equivalent to a type class, and since the family can't be extended by anyone, no other instances of the "class" can be defined.

    (This is actually just speculation, since I can't test it, but the code in this interesting link makes it look like it would work.)

提交回复
热议问题