I've installed VM as suggested, and now reading this article. Now I'm at the very beginning and looking at FFI example. I have missing ffi.jsexe/
folder (within ghcjs-examples/weblog/ffi
) and tried to compile it by myself. But it fails with following error:
setTimeout($c, $1);: invalid placeholder, check function type: "$c"
Here is quick reminder what we have in ffi.hs
:
{-# LANGUAGE JavaScriptFFI, CPP #-}
module Main where
#ifdef __GHCJS__
foreign import javascript unsafe "document.write($1+'<br/>');" writeNumber :: Int -> IO ()
foreign import javascript safe "setTimeout($c, $1);" delay :: Int -> IO ()
#else
writeNumber = error "writeNumber: only available from JavaScript"
delay = error "delay: only available from JavaScript"
#endif
main :: IO ()
main = mapM_ (\x -> writeNumber x >> delay 1000) [1..1000]
Sorry, I'm new to Haskell and FP at all, so some thing for me is just alike magic for now.
I've replaced $c
with null
because it makes sense since delay
itself have only one argument, thus there will be no code executed. So it compiles, but when I opened it in a browser all numbers are printed instantly.
Also I have a little pre-question. setTimeout
return some handler (integral value), and takes a callback function and time in ms, right? So why the delay
have this type signature Int -> IO ()
instead of, say, IO () -> Int -> IO Int
?
And the main question is why it rejects to compile? Is this example outdated and something have been changed in ghcjs
itself?
GHC 7.7.2
GHCJS 0.1.0
On the current VM, if I write
interruptible
insteadsafe
in the foreign import, it works.