Is there a sane way to apply a polymorphic function to a value of type Dynamic
?
For instance, I have a value of type Dynamic
and I want to apply Just
to the value inside the Dynamic
. So if the value was constructed by toDyn True
I want the result to be toDyn (Just True)
. The number of different types that can occur inside the Dynamic
is not bounded.
(I have a solution when the types involved come from a closed universe, but it's unpleasant.)
This is perhaps not the sanest approach, but we can abuse my
reflection
package to lie about a TypeRep.Given that we can now peek at the
TypeRep
of ourDynamic
argument and instantiate ourDynamic
function appropriately.It could be a lot easier if
base
just supplied something likeapD
for us, but that requires a rank 2 type, andTypeable
/Dynamic
manage to avoid them, even ifData
does not.Another path would be to just exploit the implementation of
Dynamic
:and
unsafeCoerce
to your ownDynamic'
data type, do what you need to do with theTypeRep
in the internals, and after applying your function,unsafeCoerce
everything back.