How to box/unbox values?

Hannah Schroeter uk1o@rz.uni-karlsruhe.de
Tue, 10 Apr 2001 09:53:19 +0200


Hello!

On Tue, Apr 10, 2001 at 01:55:21AM +0200, Thomas Pasch wrote:

> is there an easy way to box/unbox Types.
> I need this for Int's, so is there 
> a function that does:

> Int# -> Int
> Int -> Int#

You must import the appropriate modules, to "see" the type definition
for Int (which is data Int = I# Int# in GHC). Then just use plain
pattern matching:

unboxInt :: Int -> Int#
unboxInt (I# i#) = i#

boxInt :: Int# -> Int
boxInt i# = I# i#

Note that there are some restrictions for handling unboxed values,
see the documentation.

Kind regards,

Hannah.