True, so perhaps better written as:

import Data.Maybe (fromMaybe)

gets (fromMaybe (error "could not find re in resFwdMap") . M.lookup re . resFwdMap)

with more detail in error message as appropriate.

-Ross

On Jun 3, 2009, at 5:57 PM, Henning Thielemann wrote:

Ross Mellgren schrieb:
You've applied two solutions to get the value out -- pattern matching
(Just reinfo) and fromJust. You should use one or the other, but not both:

-- pattern matching
remLookupFwd :: (ReVars m t) => SimplRe t -> ReM m t (ReInfo t)
remLookupFwd re
  = do fwd <- gets resFwdMap
let { Just reinfo = M.lookup re fwd } -- PROBLEM
       return reinfo

-- fromJust
remLookupFwd :: (ReVars m t) => SimplRe t -> ReM m t (ReInfo t)
remLookupFwd re
  = do fwd <- gets resFwdMap
       let { reinfo = fromJust (M.lookup re fwd) }
-- PROBLEM
       return reinfo

BTW, I would personally write this as one line (untested)

gets (fromJust . M.lookup re . resFwdMap)

fromJust should be avoided, since it is partial and if it results in an
error, the error message points to the implementation of fromJust, not
its application. Pattern matching is better, but 'maybe' and 'fromMaybe'
are best.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to