The Dynamic
library provides cheap-and-cheerful dynamic types for
Haskell. A dynamically typed value is one which carries type
information with it at run-time, and is represented here by the
abstract type Dynamic
. Values can be converted into Dynamic
ones, which can then be combined and manipulated by the program using
the operations provided over the abstract, dynamic type. One of
these operations allows you to (try to) convert a dynamically-typed
value back into a value with the same (monomorphic) type it had before
converting it into a dynamically-typed value. If the dynamically-typed
value isn't of the desired type, the coercion will fail.
The Dynamic
library is capable of dealing with monomorphic types
only; no support for polymorphic dynamic values, but hopefully that
will be added at a later stage.
Examples where this library may come in handy (dynamic types, really - hopefully the library provided here will suffice) are: persistent programming, interpreters, distributed programming etc.
The following operations are provided over the Dynamic
type:
data Dynamic -- abstract, instance of: Show --
toDyn :: Typeable a => a -> Dynamic
fromDyn :: Typeable a => Dynamic -> a -> a
fromDynamic :: Typeable a => Dynamic -> Maybe a
toDyn
converts a value into a dynamic one, provided
toDyn
knows the (concrete) type representation of the value.
The Typeable
type class is used to encode this, overloading a
function that returns the type representation of a value. More on this
below.fromDyn
, tries to convert the dynamic value into
a value with the same type as its second argument. If this fails, the
default second argument is just returned. fromDynamic
returns a
Maybe
type instead, Nothing
coming back if the conversion
was not possible.Dynamic
type has got a Show
instance which returns
a pretty printed string of the type of the dynamic value. (Useful when
debugging).