Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Mixing variant types...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Mixing variant types...
From: Jonathan Roewen <jonathan.roewen@gmail.com>

> File "window_system.ml", line 99, characters 23-50:
> This expression cannot be coerced to type
>   ws_event = (window * [ `New_window | `Repaint ]) Event.event;
> it has type (window * event) Event.event but is here used with type
>   ws_event = (window * [ `New_window | `Repaint ]) Event.event
> Type event = [ `Repaint ] is not compatible with type
>   [ `New_window | `Repaint ]
> The first variant type does not allow tag(s) `New_window
> 
> The toplevel has just proven to me that this can be done, so I don't
> understand what's going on here.

The reason is simple: Event.event is not marked ascovariant in its
parameter. So even if t1 is a subtype of t2, this does not imply that
t1 Event.event will be a subtype of t2 Event.event.
After thinking a bit of the semantics, it seems that it would be safe
to make Event.event covariant. Of course Event.channel cannot be
made covariant, as it can be used both to send and receive values.

You can already simulate subtyping using Event.wrap:
  Event.wrap e (fun x -> (x : t1 :> t2))
will turn a t1 event into a t2 event.
(So this probably means that Event.event should be covariant to start
with...)

For practical applications, the solution suggested by skaller might be
satisfactory too.

Jacques