bos / configurator

A Haskell library supporting flexible, dynamic file-based configuration.
Other
128 stars 27 forks source link

lookupDefault and lookup are curiously lazy #23

Open treeowl opened 8 years ago

treeowl commented 8 years ago

Currently,

lookupDefault def cfg name = fromMaybe def <$> lookup cfg name

I'd have expected

lookupDefault def cfg name = maybe (return def) return =<< lookup cfg name

lookup is also a bit weird:

lookup :: Configured a => Config -> Name -> IO (Maybe a)
lookup (Config root BaseConfig{..}) name =
    (join . fmap convert . H.lookup (root `T.append` name)) <$> readIORef cfgMap

There's no obvious reason to delay the lookup to the use site (which keeps the entire HashMap live). Why not use something like this?

lookup (Config root BaseConfig{..}) name =  do
     mp <- readIORef cfgMap
     let result = convert =<< H.lookup (root `T.append` name) mp
     evaluate result
     return result
treeowl commented 8 years ago

If you'd prefer to delay the conversion, which seems somewhat less peculiar than delaying the lookup, that would be

lookup (Config root BaseConfig{..}) name =  do
     mp <- readIORef cfgMap
     let result = H.lookup (root `T.append` name) mp
     evaluate result
     return (convert =<< result)