[Haskell-cafe] How to improve below code?

Neil Brown nccb2 at kent.ac.uk
Tue Jun 9 10:21:05 EDT 2009


Andy Stewart wrote:
> So have a better solution to avoid write above ugly code
How about:

====
data Page a =
    Page {pageName      :: IORef String
         ,pageId        :: Int
         ,pageBuffer    :: a
         ,pageBox       :: VBox
         }

class PageBuffer a where
  pageBufferClone :: a -> IO (a, VBox)

pageClone :: PageBuffer a => Page a -> IO (Page a)
pageClone page = do
  -- Get common information for clone page.
  name <- pageGetName page
  let id = pageId page
      pb = pageBuffer page

  -- Get clone information for dynamic interface. 
  (pBuffer, pBox) <- pageBufferClone pb

  -- Return clone page.
  pageNewInternal name id pBuffer pBox

====

I'm not totally sure if that will work without seeing the rest of your 
code.  But it seems neater, and no GADTs in sight.  If you need to store 
Page StringBuffer in a list with Page ImageBuffer you will have a 
problem, so perhaps you could spell out what else you need to do with 
these Page items in your application?

Thanks,

Neil.


More information about the Haskell-Cafe mailing list