I have tried
map show . mapMaybe fromDynamic $ [toDyn "one", toDyn (\x -> x::Integer), toDyn 3, toDyn ()]
but it returned
["()"]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your code does not do what you expect it to do. Long before the dynamic behaviour of
Data.Dynamickicks in, the Haskell type checker resolves the types. The type of the right part of the expression isand the type of the left part is
so to combine these, the type variable
bresp.agets unified. If you were to compile this from a regular Haskell file, the compiler would give you a warning (The type variable `a' is ambigous). But in GHCi, the interpreter just defaults to().But this fixes the type of
fromDynamicin the expression toDynamic -> Maybe (), effectively selecting all elements of type().If you force the compiler to use a different type there, e.g. by specifying a type signature, you see that
fromDynamicselects a different type:Unfortunately, there is no way to achieve what you want: Select all elements whose type support a show instance, as that information is not available to
fromDynamic.