[Haskell-beginners] private types

matthew coolbeth mac01021 at engr.uconn.edu
Thu Apr 8 16:06:33 EDT 2010


A variant on Stephen's solution is to use Haskell's record syntax, which
will generate the "destructors" implicitly.  This is no easier on your
users, but much easier for you to write, depending on the structure of your
type.

On Thu, Apr 8, 2010 at 15:54, Ashish Agarwal <agarwal1975 at gmail.com> wrote:

> This makes T fully abstract, which is what I'm trying to avoid. Private
> types provide an intermediate level of abstraction. The real example I have
> involves defining a type with numerous constructors (or a record with
> numerous fields), and I'd like to avoid writing all the corresponding
> destructors. Even if I did do this, it burdens clients of the module by
> requiring them to use the alternative destructors.
>
> I think the feature I'm looking for is "guarded constructors", but to my
> knowledge there is no such thing.
>
>
> On Thu, Apr 8, 2010 at 3:42 PM, Stephen Tetley <stephen.tetley at gmail.com>wrote:
>
>> 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
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


-- 
mac
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100408/22fdd4e0/attachment-0001.html


More information about the Beginners mailing list