MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16487.52466.85193.17461 at soggy.deldotd.com> Date: Sun, 28 Mar 2004 23:14:58 -0800 To: Jacques Garrigue Cc: lablgtk at kaba.or.jp Subject: Re: event variant types In-Reply-To: <20040329112224Z.garrigue at kurims.kyoto-u.ac.jp> References: <16486.17988.561211.516751 at soggy.deldotd.com> <20040328191235E.garrigue@kurims.kyoto-u.ac.jp> <16487.13929.266175.311338@soggy.deldotd.com> <20040329112224Z.garrigue@kurims.kyoto-u.ac.jp> From: briand at aracnet.com >>>>> "Jacques" == Jacques Garrigue writes: Jacques> let key_code = GdkEvent.Key.keyval (GdkEvent.Key.cast event) in And this works ! Thank You. The code now looks like : let handle_event event = (* type coercion I think... *) let event = (event :> GdkEvent.any) in match GdkEvent.get_type event with #GdkEvent.Button.types -> Printf.printf "button press\n"; true | `KEY_PRESS -> let key_code = GdkEvent.Key.keyval (GdkEvent.Key.cast event) in true (* | `BUTTON_PRESS -> Printf.printf "button press\n"; true *) | `MOTION_NOTIFY -> Printf.printf "motion\n"; true | _ -> false Jacques> Of course the above pattern-matching guarantees that event is a key Jacques> event, but the type system is not strong enough to see that. Jacques> Thinking of this, this should be possible to do it with private types Jacques> and variant dispatch, but this is getting complicated... ??? I don't know what this means. But if it does not make things simpler ... I have one last question, at it is maybe a put ocaml question. It is VERY strange. The following code WORKS let p1 = ref { x=0.; y=0.; z=0. };; let draw_polyline_interactively = fun area drawv event -> let event = (event :> GdkEvent.any) in ... However when I try to establish p1 as a closure variable : let draw_polyline_interactively = let p1 = ref { x=0.; y=0.; z=0. } in fun area drawv event -> let event = (event :> GdkEvent.any) in ... I get an error at the _calling_ location (not in the function declaration) : This expression has type GdkEvent.Button.t = GdkEvent.Button.types Gdk.event but is here used with type GdkEvent.Key.t = [ `KEY_PRESS | `KEY_RELEASE ] Gdk.event Type GdkEvent.Button.types = [ `BUTTON_PRESS | `BUTTON_RELEASE | `THREE_BUTTON_PRESS | `TWO_BUTTON_PRESS ] is not compatible with type [ `KEY_PRESS | `KEY_RELEASE ] These two variant types have no intersection How could the (seemingly unrelated) local variable possibly impact the treatment of event ??? The error ONLY occurs when I try to use the p1 variable, any other declaration has no effect. I simply need to move p1 outside, into the top level, changing nothing else and everything is fine. Sorry to be such a pest. I seem to have picked a difficult topic to start with in ocaml :-( Brian