[Haskell-cafe] Calling function with unknown number and type arguments

Maciej Piechotka uzytkownik2 at gmail.com
Sun Aug 16 06:45:42 EDT 2009

I have to write a function which calls a function with unknown (i.e.
given in runtime) number and type of arguments.

1. Use closure implementation from Gtk2Hs.
Pros: Written
Cons: Not in haskell

2. Use unsafeCoerce.
Something like:
f a b = return (a + b)
f' = unsafeCoerce (f :: Int -> Int -> IO Int) :: IO ()

unsafeCoerce (unsafeCoerce ((unsafeCoerce f' :: Int -> IO ()) 5) :: Int
-> IO ()) 4 :: IO Int

(of course it is an example in fact it will be not only Int etc.)

Personally I'd prefere in-haskell solution as:
- Rts API used by Gtk2Hs is 'semi-public'
- It is GHC-specific (?)
but I'm not sure if my solution is safe.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090816/0ee74bcb/attachment-0001.bin

More information about the Haskell-Cafe mailing list