[Haskell-beginners] private types

Stephen Tetley stephen.tetley at gmail.com
Thu Apr 8 15:42:22 EDT 2010


Hi

I don't think there is a direct equivalence. You can make T an opaque
type using a module where you do not export the constructor. However,
you can't then pattern match on the type so you have to supply a
destructor - here called unT. Any module that imports XYZ only sees
the type T, the 'handmade' constructor makeT and the destructor unT:



module XYZ
  (
    T,         -- this does not export the constructor
    unT
    makeT

  ) where

data T = PrivateT Int   deriving (Eq,Show)

unT :: T -> Int
unT (PrivateT i) = i

-- Add any checking you want here...
makeT :: Int -> T
makeT i = PrivateT i


More information about the Beginners mailing list