Donald Bruce Stewart wrote:

> Depending on how evil you are, you can already do this. Call the show,
> and if it doesn't exist you can catch the exception that is thrown 

Brilliant. By the way, this idea also solves the problem
of not being able to define defaults for record fields, like so:

import Control.Exception

data Thing = Thing { foo :: Int, bar :: Int } deriving Show

wrap :: Thing -> IO Thing
wrap x =
    ( foo x `seq` bar x `seq` return x )
    `Control.Exception.catch` \ _ ->
    ( foo x `seq` return $ x { bar = 2 } )
    `Control.Exception.catch` \ _ ->
    ( bar x `seq` return $ x { foo = 4 } )
    `Control.Exception.catch` \ _ ->
    return $ Thing { foo = 4, bar = 2 }

main = do
    wrap ( Thing { foo = 5 } ) >>= print
    wrap ( Thing { bar = 7 } ) >>= print
    wrap ( Thing {         } ) >>= print
-- 
-- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 --
---- http://www.imn.htwk-leipzig.de/~waldmann/ -------

_______________________________________________
Haskell-prime mailing list
Haskell-prime@haskell.org
http://haskell.org/mailman/listinfo/haskell-prime

Reply via email to