On Tue, 2009-02-03 at 15:03 -0200, Marco Túlio Gontijo e Silva wrote:
> Hello,
> 
> I was kind of bothered in having to pass castTo* functions to
> xmlGetWidget, and I thought that this could be done easier with a type
> class:
> 
> > class Cast widget where
> >   cast :: GObjectClass obj => obj -> widget
> 
> > instance Cast Window where
> >   cast = castToWindow
> 
> > instance Cast ImageMenuItem where
> >   cast = castToImageMenuItem
> 
> > getWidget
> >   :: (Cast widget, WidgetClass widget) => GladeXML -> String -> IO
> widget
> > getWidget glade name = xmlGetWidget glade cast name
> 
> After having the GladeXML, I'll do something like:
> 
>     let
>       getWidget' :: (Cast widget, WidgetClass widget) => String -> IO
> widget
>       getWidget' = getWidget glade
> 
> Is it bad to have this Cast class in gtk2hs?

Well, it's a bit of a hen-and-egg thing. You got to tell the compiler
eventually what widget you'd like 'widget' to be. Since most (all?)
methods in Gtk2Hs are able to take more derived widgets, (e.g. even
ComboBoxEntry which is a leaf has functions like, uh, 

comboBoxEntryDoSomething :: ComboBoxEntryClass self => self -> Int -> IO
()

simply calling a method will not be enough to pin down the type. It's
like trying to compile (show . read) where the intermediate type is also
ambiguous. Thus, using your class Cast, you'd still need to add a type
signature somewhere. This might be less of a hassle than using a
specific castToComboBoxEntry, but only in some cases. Unless you have a
very good example where the Cast class would be simpler than a castToFoo
function, I'd rather not add yet another layer of type classes.

Cheers,
Axel.



------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Gtk2hs-devel mailing list
Gtk2hs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gtk2hs-devel

Reply via email to