Hi,
I'm trying to implement a transparent cache that can load a specific object when it's not available, or return an already existing object indexed by (name
).
I've tried running this:
loader' objs name = case findObj objs name of
Nothing → (new_obj, loader' new_objs)
Just obj → (obj, loader' objs)
where
new_objs = [(name, new_obj)] ++ objs
new_obj = readObj name
loader = loader' []
but I'm getting a
Occurs check: cannot construct the infinite type: t = (ObjType, String -> t)
which looks exactly like something I want :)
How can I fix the function so that it compiles?
Clarifications:
As requested, signatures (findObj either returns a known value found via a key, or Nothing, readObj creates a new obj for a key):
findObj :: [(String, ObjType)] -> String -> Maybe ObjType
readObj :: String -> ObjType
And yes - I need a cache, because the keys are filenames and I don't want to read+parse the file each time some object is needed.