Data.Dynamic (original) (raw)

Contents

Description

The Dynamic interface provides basic support for dynamic types.

Operations for injecting values of arbitrary type into a dynamically typed value, Dynamic, are provided, together with operations for converting dynamic values into a concrete (monomorphic) type.

Synopsis

Documentation

The Dynamic type

data Dynamic Source

A value of type [Dynamic](Data-Dynamic.html#t:Dynamic) is an object encapsulated together with its type.

A [Dynamic](Data-Dynamic.html#t:Dynamic) may only represent a monomorphic value; an attempt to create a value of type [Dynamic](Data-Dynamic.html#t:Dynamic) from a polymorphically-typed expression will result in an ambiguity error (see [toDyn](Data-Dynamic.html#v:toDyn)).

[Show](Text-Show.html#t:Show)ing a value of type [Dynamic](Data-Dynamic.html#t:Dynamic) returns a pretty-printed representation of the object's type; useful for debugging.

Converting to and from Dynamic

toDyn :: Typeable a => a -> DynamicSource

Converts an arbitrary value into an object of type [Dynamic](Data-Dynamic.html#t:Dynamic).

The type of the object must be an instance of [Typeable](Data-Typeable-Internal.html#t:Typeable), which ensures that only monomorphically-typed objects may be converted to[Dynamic](Data-Dynamic.html#t:Dynamic). To convert a polymorphic object into [Dynamic](Data-Dynamic.html#t:Dynamic), give it a monomorphic type signature. For example:

toDyn (id :: Int -> Int)

fromDynSource

Arguments

:: Typeable a
=> Dynamic the dynamically-typed object
-> a a default value
-> a returns: the value of the first argument, if it has the correct type, otherwise the value of the second argument.

Converts a [Dynamic](Data-Dynamic.html#t:Dynamic) object back into an ordinary Haskell value of the correct type. See also [fromDynamic](Data-Dynamic.html#v:fromDynamic).

Applying functions of dynamic type