Version française
Home     About     Download     Resources     Contact us    
Browse thread
Yet another question about insufficient polymorphism
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Yet another question about insufficient polymorphism
[Hope you don't mind this but I've CC'd back to caml-list ...]

On Wed, Mar 12, 2008 at 10:57:46PM +0100, Olivier Andrieu wrote:
> On Wed, Mar 12, 2008 at 9:59 PM, Richard Jones <rich@annexia.org> wrote:
> > On Wed, Mar 12, 2008 at 09:19:32PM +0100, Olivier Andrieu wrote:
> >  > aren't you missing a variance annotation ? like :
> >  >
> >  > type -'a component
> >
> >  I don't know because no one has ever been able to explain covariance &
> >  contravariance to me in a comprehensible way !
> 
> there's this post where I tried to explain that to someone:
> http://groups.google.com/group/fa.caml/msg/584ea2139b69f0ed
> 
> >  I added the '-' but it doesn't appear to have fixed the problem
> >  unfortunately.
> 
> You'll need it anyway I think :)
> What's the type of your functions that create components ? (textbox
> and button ?)

val button : int -> int -> string -> [ `Button | `Component ] component

val textbox : int -> int -> int -> int -> flag list ->
    [ `Component | `Textbox ] component

> >  Someone mentioned that it might be because when I added the signature,
> >  the type became abstract (before, the implementation of the type was
> >  exposed).  I can't make that work either though, but maybe because my
> >  type is slightly different than it was before ...
> 
> You'll probably have to add some coercions , like:
> 
>   let as_co c = (c :> [`Component] component)
> 
> and then
> 
>   form_add_components form [as_co text; as_co b1; as_co b2];

Right, this is very confusing.  Adding the -'a annotation:

  type -'a component

and:

  let as_co c = (c :> [`Component] component) in
  form_add_components form [as_co text; as_co b1; as_co b2];              

works.

However omitting the minus in "type -'a ..." breaks it, and changing
the list to:

  ([text; b1; b2] :> [`Component] component list)

doesn't work either.

I guess I'll need to read about co-/contra-variance.

Rich.

-- 
Richard Jones
Red Hat