[Haskell-beginners] private types

Ashish Agarwal agarwal1975 at gmail.com
Thu Apr 8 15:11:23 EDT 2010


Is there a Haskell analogue to OCaml's private types? In OCaml, you can do
this (converting to pseudo-Haskell syntax):

type T = private A Int

Then, A cannot be used directly to construct values of type T.
A 3
Error: Cannot create values of the private type T

You must provide another constructor, makeT, which takes an Int and returns
a T. This allows you to do additional checks on the Int argument. However,
you can still deconstruct values of type T.

f (A x) = x + 2

This works even when f is defined in a different module. The benefit is that
you can restrict the allowed values of T, but still have the convenience of
using existing operations on these values once constructed.

The solution that comes to mind is to make T abstract and an instance of
some appropriate class, but there is no class that lets you pattern match on
arbitrary variant types.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100408/2ef22442/attachment.html


More information about the Beginners mailing list