MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16487.13929.266175.311338 at soggy.deldotd.com> Date: Sun, 28 Mar 2004 12:32:41 -0800 To: Jacques Garrigue Cc: lablgtk at kaba.or.jp Subject: Re: event variant types In-Reply-To: <20040328191235E.garrigue at kurims.kyoto-u.ac.jp> References: <16483.50834.898498.682875 at soggy.deldotd.com> <16483.63571.455639.187324@akasha.ijm.jussieu.fr> <16486.17988.561211.516751@soggy.deldotd.com> <20040328191235E.garrigue@kurims.kyoto-u.ac.jp> From: briand at aracnet.com >>>>> "Jacques" == Jacques Garrigue writes: Jacques> From: briand@aracnet.com >> I've created a simpler test program. >> >> here is some relevant code: >> >> let handle_event event = >> match GdkEvent.get_type event with >> (* #GdkEvent.Button.types -> handle_button_event event; true *) >> `KEY_PRESS -> Printf.printf "key press\n"; true >> | `MOTION_NOTIFY -> Printf.printf "motion\n"; true >> | _ -> false Jacques> Sorry, using variant polymorphism can get tricky if you Jacques> want to keep type safety. Thanks you for your continued help. I'm new to ocaml/ml it's taking a while to get used to the type system. 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. So it seems that I need to coerce event once again to extract event-specific information. Unfortunately my lack of type understanding is a problem. I've tried this via several methods most of which result in syntax errors or other errors. So here I am on the list. BTW, would it be simpler to add the "types" variable which is present in the button press event to all of the events ? I' d be happy to implement this and send patches, it would be little work. Thanks Brian The error message: This expression has type GdkEvent.any = Gdk.Tags.event_type Gdk.event but is here used with type GdkEvent.Key.t = [ `KEY_PRESS | `KEY_RELEASE ] Gdk.event Type Gdk.Tags.event_type = [ `BUTTON_PRESS | `BUTTON_RELEASE | `CLIENT_EVENT | `CONFIGURE | `DELETE | `DESTROY | `DRAG_ENTER | `DRAG_LEAVE | `DRAG_MOTION | `DRAG_STATUS | `DROP_FINISHED | `DROP_START | `ENTER_NOTIFY | `EXPOSE | `FOCUS_CHANGE | `KEY_PRESS | `KEY_RELEASE | `LEAVE_NOTIFY | `MAP | `MOTION_NOTIFY | `NOTHING | `NO_EXPOSE | `PROPERTY_NOTIFY | `PROXIMITY_IN | `PROXIMITY_OUT | `SCROLL | `SELECTION_CLEAR | `SELECTION_NOTIFY | `SELECTION_REQUEST | `SETTING | `THREE_BUTTON_PRESS | `TWO_BUTTON_PRESS | `UNMAP | `VISIBILITY_NOTIFY | `WINDOW_STATE ] is not compatible with type [ `KEY_PRESS | `KEY_RELEASE ]