From: John L. <jw...@gm...> - 2009-08-25 06:21:26
|
What do you find ugly about it? I think it's pretty elegant myself. Anyway, there is a third option, that isn't quite full existential types. data GridWidget = GwLabel Label | GwTextView TextView gridWidgetNew :: String -> IO GridWidget gridWidgetNew str = case str of "GwLabel" -> liftM GwLabel $ labelNew Nothing "GwTextView" -> liftM GwTextView textViewNew _ -> error "Invalid GridWidget" Rather than matching on strings, you could use another type to represent the names and pattern match on that. John On Mon, Aug 24, 2009 at 9:02 PM, Magicloud Magiclouds<mag...@gm...> wrote: > I thought about this before, in another case, but still, I cannot > belive I should do it this ugly.... > > On Tue, Aug 25, 2009 at 12:15 AM, John Lato<jw...@gm...> wrote: >> Hello, >> >> For this, I don't think you'd need existentials. First of all, I >> would use different names for the GridWidget constructors since Label >> and TextView are already used by gtk. >> >> data GridWidget = GwLabel >> | GwTextView >> >> and I'd probably derive Eq, Read, and Enum as well. >> >> Next point, you can't have gridWidgetNew return a polymorphic type >> like this. See http://www.haskell.org/haskellwiki/OOP_vs_type_classes >> for more detail. Even if you use existential types, you'll still need >> to wrap it in another constructor. If you don't need the child other >> than simply adding it to the parent widget, I would use the following >> function: >> >> addGridWidget :: ContainerClass c => c -> GridWidget -> IO c >> addGridWidget self GwLabel = do >> child <- labelNew Nothing >> set self [ containerChild := child ] >> return self >> addGridWidget self GwTextView = do >> child <- textViewNew >> set self [ containerChild := child ] >> return self >> >> This will work without existential types because the type of the child >> widget doesn't escape the function. >> >> Cheers, >> John >> >>> From: Magicloud Magiclouds <mag...@gm...> >>> >>> I have read the wiki, and I think gtk2hs's way is enough for me. >>> The other part of my code would be like this: >>> >>> myWidgetNew gridWidget = do >>> self <- someWidgetNew >>> child <- gridWidgetNew gridWidget >>> set self [ containerChild := child ] >>> return self >>> >>> Well, it failed compiling on gridWidgetNew definition. >>> >>> On Mon, Aug 24, 2009 at 8:19 PM, Bulat >>> Ziganshin<bul...@gm...> wrote: >>>> Hello Magicloud, >>>> >>>> Monday, August 24, 2009, 1:39:48 PM, you wrote: >>>> >>>>> ? My total purpose is to create a widget with an argument that what >>>>> child widget the user wants to have. So I pass GridWidget to the >>>>> widget init function, and it calls gridWidgetNew to see what new >>>>> function it actually has to call. >>>> >>>> this depends on how you plan to use the widget later. Haskell provides >>>> existential types for such things, but Gtk2Hs own mechanisms may be >>>> also useful >>>> >>>> read about existentials here: http://haskell.org/haskellwiki/OOP_vs_type_classes >>>> >> > > > > -- > 竹密岂妨流水过 > 山高哪阻野云飞 > |