
2008/11/4, Antoine Latter
On Mon, Nov 3, 2008 at 4:58 PM, minh thu
wrote: Hi,
Given a TypeRep and a Dynamic value with type corresponding to the TypeRep, I'd like to be able to use fromDyn *without* specifying a type (given as an additional 'default' argument) (since it is somewhat known from the TypeRep).
That is, I'd like an undefinedOf that can be used in
extract :: Typeable a => TypeRep -> Dynamic -> a extract typerep dyn = fromDyn (undefinedOf typerep) dyn
For instance, if TypeRep is Int, extract Int dyn = fromDyn (undefined::Int) dyn
Is it possible to write "extract" ?
Yep! It's already written:
fromDynamic :: Typable a => Dynamic -> Maybe a
It returns Nothing if the Dynamic value was of the wrong type.
See:
http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Dynamic.html#v:f...
Well, it seems you can't really write (fromJust . fromDynamic) someDynamic without giving the type of what you expect. fromDynamic whould achieve the same thing I wrote in the previous mail if I did not write the ::Int on the right of undefined. (I would still have to write ::Int later in the code; that's what I'd like to avoid) Thanks, Thu