Closed type classes

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

    You could refactor the typeclass into a data declaration (use record syntax) which contains all the functions your typeclass had. A fixed finite list of instances sounds like you don't need a class anyway.

    This is of course essentially what the compiler is doing behibd the scenes with your class anyway.

    This would allow you to export the list of instances as functions to your data type, and you can export them but not the constructors for the data type. Similarly, you can restrict exporting of the accessor functions, and just export the interface you actually want.

    This works fine because data types aren't subject to the module-boundary-crossing open world assumption the typeclasses are.

    Sometimes adding typesystem complexity just makes things harder.

提交回复
热议问题