On Tue, Nov 10, 2009 at 5:04 AM, Philippos Apolinarius <phi50...@yahoo.ca> wrote: > > foreign import ccall "rs232.h opencport" opencport :: CInt -> IO () > foreign import ccall "rs232.h closecport" closecport :: CInt -> CInt > [...] > > Originally, I had the following line (that did not work properly): > > foreign import ccall "rs232.h closecport" closecport :: IO () >
I don't know why the latter line didn't work properly, but I'm pretty sure it's closer to the right answer than the former. If you don't have an IO type for your function, then Haskell is allowed to assume it is pure (has no side effects) and can then call it only when the result is needed, or multiple times if it likes, without affecting the meaning of the program. For a function that closes a handle this is clearly not the case. So I'm pretty sure your type signature needs to be in IO if you want to guarantee it is called at the right time; it might be worth elaborating on how the IO () version did not work, and how you used it. The way you are using it now would appear to work most of the time because the print statement will force the result to be evaluated, forcing the function to be called - but having a handle closed based on when an apparently irrelevant print statement runs or doesn't is obviously not ideal. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe