From: Axel S. <Axe...@en...> - 2009-05-12 18:53:47
|
On May 12, 2009, at 20:27, Maurício wrote: >>>> class Cast widget where >>>> cast :: GObjectClass obj => obj -> widget >>>> instance Cast Window where >>>> cast = castToWindow >>>> (...) > >> (...) 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. > > My package 'gtk2hs-rpn' would use that. Since it's a RPN > calculator for widgets, it uses a stack (list, actually) > of widgets, casted at runtime. I do that using a CanBeCasted > module inside the package. > > I don't know if this qualify as a very good example, though. > One drawback of these classes is that they are highly ambiguous: You cannot use the 'cast' function without also providing an explicit type signature. In particular, you might as well insert the castToWindow function directly wherever you use 'cast w :: Window'. In your implementation you have, e.g.: pBPED = RPN $ let { bped :: Box -> Widget -> IO () ; bped = boxPackEndDefaults } in pullWidget bped ; If I'm not mistaken, you could change pullWidget to take a castToBox function to achieve the same effect. Cheers, Axel. |