On 2004-04-28T15:12:03-0400, S. Alexander Jacobson wrote:
> Ok, but it sounds like you need to know the list
> of possible types in advance.  Is it possible
> to have a lib take a filepath and type name as an
> arbitrary string, and read the instance in?

I don't think you need to know the list of possible types in advance.
Here is some (only slightly tested) code:

    import Data.Dynamic
    import Maybe
    import Monad

    lookupRead :: (Eq key, Read val, Typeable val) =>
        key -> [(key, (String, String))] -> Maybe val
    lookupRead key list = ret
      where ret = case lookup key list of
                    Just (typ, val) ->
                      if typ == show (typeOf (fromJust ret))
                        then case reads val of
                               [(v,"")] -> Just v
                               _        -> Nothing
                        else Nothing
                    _ -> Nothing

The question is how to get the result of lookupRead memoized, so that
the call to reads happens at most once for each entry in the list.

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
BBC News: Universities face week of protest
http://news.bbc.co.uk/1/hi/education/3508209.stm

Attachment: pgp00000.pgp
Description: PGP signature

_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to