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: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] Mixing variant types...
I've played with coercing to a wider type in the toplevel, and the
following works fine:

# type a = [ `Repaint ];;
type a = [ `Repaint ]
# type b = [ `New_window ];;
type b = [ `New_window ]
# let f () : a = `Repaint;;
val f : unit -> a = <fun>
# let g () : b = `New_window;;
val g : unit -> b = <fun>
# let l = [ (f :> (unit -> [`Repaint|`New_window])); (g :> (unit ->
[`Repaint|`New_window]))];;
val l : (unit -> [ `New_window | `Repaint ]) list = [<fun>; <fun>]
# List.map (fun f -> f ()) l;;
- : [ `New_window | `Repaint ] list = [`Repaint; `New_window]
# let ll = [ ((f ()) :> [`Repaint|`New_window]); ((g ()) :> [a|b]) ];;
val ll : [ `New_window | `Repaint ] list = [`Repaint; `New_window]

So, it all works fine & dandy. So I'm trying to duplicate this with my
window system problem.

I have:

(* Module Channel *)
type ('a,'b) channel = {
	source: 'a;
	chan: ('a * 'b) Event.channel;
}

let send channel value = Event.send channel.chan (channel.source, value)

let receive channel = Event.receive channel.chan

(* Module Window_system *)
type event = [ `Repaint ]
type control = [ `New_window ]
type window = {
	mutable bounds: bounds;
	
	surface: Cairo.image_surface;
	
	mutable clips: bounds list;
	
	control: (window, event) Channel.channel;
}

type ws_event = (window * [event | control]) Event.event

let create left top width height =
  let rec window = {
    ...
    control = { source = window; chan = Event.new_channel (); };
  }

let run () =
  while true do
    let sources =
      ((Event.receive internal_control) :> ws_event) ::
      List.map (fun w -> ((Channel.receive w.control) :> ws_event))
!windows in (* Line 99 *)
    match Event.select sources with
    | source, `Repaint -> repaint source
    | _ -> ()
  done

And I get the following error:

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.

Kindest Regards,

Jonathan Roewen