Wonderful thing about Haskell. The type of a function almost dictates its implementation. That’s the case for this one, but… my brain just isn’t wrapping around the nested function thing here:
mkDyn :: (Typeable a) => ((a -> IO()) -> IO ()) -> ((Dynamic -> IO()) -> IO ())
The only question is how to handle error handling in the fromDynamic call that will be required, but… I can deal with that once I have the rest figured out. I’m guessing there will need to be something like the following somewhere. But I can’t seem to get the wrapper lambda stuff figured out.
case fromDynamic x of
Just x -> f x
Nothing -> undefined -- TODO
I think you want
toDyn, notfromDynamic. So let’s do this slowly:Our return type should be
IO ()and we can obtain that either by callingkorf. Callingfdoesn’t help us much, because we would somehow materialise aDynamic, but we cannot do that (sensibly) fromk. So we want to callk.kneeds another function as its argument, so lets start like this:So the function’s argument is
Typeable a => a -> IO (). We don’t have a function of that type, but we have a function of typeDynamic -> IO (). Because of theTypeableconstraint we can usetoDynto turn ouraintoDynamicand get:There are simpler implementations (e.g.,
return ()ork (\a -> return ()), but this one appears to make sense.