Converting between boxed and unboxed types

Keith Wansbrough Keith.Wansbrough@cl.cam.ac.uk
Tue, 12 Mar 2002 11:23:13 +0000


> I would like to print an unboxed value to the screen.  But to use "show" on
> a type it has to be boxed.  How do I rebox an unboxed type? Sorry if the
> answer to this is obvious - I have spent quite some time looking for the
> solution.

You need to know what kind of box - the unboxed value no longer has the `tag' saying what type it is.

Boxed Ints, for example, are declared like this in the prelude:

data Int = I# Int#

So to build an Int from an Int#, simply say  (I# x), like this:

case (+# 1# 42#) of {
  x -> show (I# x)
}

(you'll probably need to import whichever module has the definition of Int in it).

Hope this helps..

--KW 8-)
-- 
Keith Wansbrough <kw217@cl.cam.ac.uk>
http://www.cl.cam.ac.uk/users/kw217/
University of Cambridge Computer Laboratory.