
I don't think this works. I just tried it with: main = print $ lookupRead 1 [(1,("Integer","100"))] How would Haskell know that typ actually does equal typeOf? -Alex- _________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com On Wed, 28 Apr 2004, Chung-chieh Shan wrote:
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