faylang / fay

A proper subset of Haskell that compiles to JavaScript
https://github.com/faylang/fay/wiki
BSD 3-Clause "New" or "Revised" License
1.29k stars 86 forks source link

Callback feature request #414

Closed dluciv closed 9 years ago

dluciv commented 9 years ago

I could miss something.

Fay provides excellent ffi to access foreign functions. Results of all of them are then wrapped into Fay monad and everything looks just the same as for Haskell. But...

This all works well for blocking API calls, while JavaScript itself often relies on continuation passing style that it alike Haskell's one except callback invokation order. Genuine Haskell invokes blocking POSIX (and so on) API and it looks as a pain (Haskell looks too advanced for such a legacy environment). JavaScript asynchroneous APIs do look much more natural to be integrated with Haskell.

For Fay (and for any other Haskell-JavaScript compiler too) it is very sad to use API in following way (excample from fay-dom source):

setReadyStateHandler :: (XMLHttpRequest -> Fay ()) -> XMLHttpRequest -> Fay XMLHttpRequest

For Haskell it could be much more natural to write:

do
  ...
  Just result <- sendXmlHttpRequest ... params ...
  ...

There are a lot of other examples surely. At least, all Node.js input is handled similarly. Here is what can be done. But why we need palying with Cont monad and in the same time having Fay monad already handling the callbacks (just because it is a monad)?..

Can we upgrade ffi and Fay >>= operator in some way to make this built-infeature and reduce such an overhead?

bergmark commented 9 years ago

This is a cool idea!

Braindump:

agocorona commented 9 years ago

That is what the hplayground monad does

https://github.com/agocorona/hplayground

http://tryplayg.herokuapp.com/

I would be great to have it native in Fay

2014-10-06 13:00 GMT+02:00 Adam Bergmark notifications@github.com:

This is a cool idea!

Braindump:

  • Can this be added in a backwards compatible way? With just one Monad we can't detect whether something is a continuation, every non-continuation needs to be wrapped.
  • We should probably add a separate continuation ffi function, and change the normal one to just call the success handler to not break code.
  • Is there a significant overhead in making everything a continuation?
  • Can a compiler flag be added to toggle this? Would that require a separate runtime?
  • Your gist seems to be using an old version of fay/runtime.js

— Reply to this email directly or view it on GitHub https://github.com/faylang/fay/issues/414#issuecomment-58001965.

Alberto.

dluciv commented 9 years ago

The above comment (on HPlayground) probably describes just the thing wanted, yes! =)

bergmark commented 9 years ago

Spring cleaning. Lack of activity, it's unlikely that this will happen.