Ian Jackson <[EMAIL PROTECTED]> writes:
> 
> Simon Marlow writes ("RE: Change to Posix.lhs"):
> > Should we use a Maybe rather than an exception here?  
> > Probably, but then System.getVar doesn't return a Maybe,
> > and the Posix library as a whole has a tendency towards
> > exceptions rather than Maybe returns.  So I'm swithering(*)
> > on this one.
> 
> The most common use for environment variables is to allow built-in
> defaults to be overridden, and in this case they are (obviously)
> optional.
> 
> Requiring the programmer to do tedious exception-handling for
> nonexceptional conditions seems like a bad idea.  I agree with Volker
> Stolz that nearly no-one would want to use the exception-throwing
> version (though it should probably be left in for those rare cases).
> 
> Using Maybe makes it easy to provide compiled-in defaults using
> fromMaybe.
> 

But if the environment block lookup and the decision on what to do
in case it fails almost always go hand in hand, why use Maybe values
at all? Here's what I normally use:

   myGetEnv :: String -> String -> IO String
   myGetEnv defVal key = 
       catch (System.getEnv key)
             (\ _ -> return defVal)
               -- we 'know' that getEnv can only raise
               -- a NoSuchThing. 

Not particularly onerous to catch the exception here. However, I agree
with the principle - prefer Maybe values over IO exceptions, as the
former will force you to write more robust code.

--sigbjorn

Reply via email to