From: Duncan C. <dun...@wo...> - 2003-12-11 18:10:43
|
All, I've made the change to the TypeGenerator program so people who track the cvs version should probably cvs update, rebuild and make sure I've not broken anything. You'll need to $ touch gtk2hs/tools/hierarchyGen/hierarchy.list (or make clean) as the gtk directory only depends on hierarchy.list, not the TypeGenerator program as well (this is perhaps something we should fix). The only compatibility problem that I can foresee is if in any programs you've written import Gtk (Button(..)) and expect to get fromButton & toButton. Since these are not class members anymore you'd have to change that to import Gtk (Button, fromButton, toButton) I've not removed the from<type> functions. If we are agreed that it would be a good idea to do so then I'll do that. As an alternative, we could keep the functions but make them typesafe so that they can be safely exported to the user. A quick recap; the from<type> functions (eg fromButton) perform downcasts in the object hierarchy. fromButton :: ButtonClass obj => Button -> obj This is not a typesafe operation. A solution as in OO languages is to perform a dynamic type check to make sure that the cast is permitted. That is what the castTo<type> functions do, though they have a different type signature: castToButton :: GObjectClass obj => obj -> Button The second form casts from any class to a Button while the first form casts a Button to any derived class. We cannot implement the first form with the current object hierarchy infrastructure because we cannot get a representation of the type for an arbitrary object type. If we want to be able to safely perform the first kind of cast we can add a method to the GObject class that gets us the glib type representation for a given object type. Instances for all derived classes can be generated by the TypeGenerator program. It would look like so: class GObjectClass o where toGObject :: o -> GObject fromGObject :: GObject -> o getGType :: o -> GType instance GObjectClass Button where toGObject = ... fromGObject = ... getGType = {#call fun unsafe gtk_button_get_type#} Duncan |