[Haskell] Deferred instance declarations (serialization of
existential boxes)
Misha Aizatulin
avatar at hot.ee
Wed Sep 27 16:08:05 EDT 2006
Simon Peyton-Jones wrote:
> Concerning your application
>
> | I am having a box like
> | > data Box = forall a. Cxt a => Box a
> | and want to write a Read instances for it.
>
> I don’t see how it helps to defer the Read instance.
I would defer the instance declaration till the point where I know all
types that will ever go into the box (for instance in my Main module).
The Show instance of Box would write the representation of a together
with it's type (this means Cxt should contain at least Show and
Typeable). The Read instance will then contain something like
========================================================
String aType <- lexP
result <- case aType of
"[Int]" ->
(readPrec :: ReadPrec [Int]) >>= (return . Box)
"SomeOtherType" ->
(readPrec :: ReadPrec SomeOtherType) >>= (return . Box)
-- <more of such cases>
_ -> error (aType ++ " cannot be read inside of Box")
========================================================
Not very elegant of course, but it seems to be the only way to Read
existentials.
> But I think the case is stronger for top-level constraints, and I will bear it in mind.
> If you would like to open a Trac feature request, please do so.
Ok, I will.
Cheers,
Misha
More information about the Glasgow-haskell-users
mailing list