To: briand at aracnet.com Cc: lablgtk at kaba.or.jp Subject: Re: event variant types In-Reply-To: <16487.13929.266175.311338 at soggy.deldotd.com> References: <16486.17988.561211.516751 at soggy.deldotd.com> <20040328191235E.garrigue@kurims.kyoto-u.ac.jp> <16487.13929.266175.311338@soggy.deldotd.com> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20040329112224Z.garrigue at kurims.kyoto-u.ac.jp> Date: Mon, 29 Mar 2004 11:22:24 +0900 From: Jacques Garrigue Lines: 42 From: briand at aracnet.com > Jacques> There are two solutions: > Jacques> * either replace "_" by "#Gdk.Tags.event_type" in the above > Jacques> * or add the following line at the beginning of handle_event > Jacques> let event = (event :> GdkEvent.any) in > > Jacques> This is the most robust solution, as it will work even if > Jacques> you change handle_event. > > yes - the coercion works well. So the above code is now clean. > > However, we would now like to use the information to obtain keycodes > and such : > > match GdkEvent.get_type event with > (* #GdkEvent.Button.types -> handle_button_event event; true *) > `KEY_PRESS -> > let key_code = GdkEvent.Key.keyval event in > handle_key_event key_code; true > (* | `BUTTON_PRESS -> Printf.printf "button press\n"; true *) > | `MOTION_NOTIFY -> Printf.printf "motion\n"; true > | _ -> false > > The error message, which is somewhat lengthy is included below. Now > I'm even more confused. Since the key_code expression is more > "narrow" than the event type, I would not expect ocaml to complain. This is just the opposite: you have first coerced your key event to its supertype (which is safe), but you are now trying to see it as a key event again, while this function actually accepts any kind of event. This is clearly unsafe in general. You have an (unsafe) function GdkEvent.Key.cast to do that. let key_code = GdkEvent.Key.keyval (GdkEvent.Key.cast event) in Of course the above pattern-matching guarantees that event is a key event, but the type system is not strong enough to see that. Thinking of this, this should be possible to do it with private types and variant dispatch, but this is getting complicated... Jacques