TL;DR: I wrote a tool for javascript interpolation (inline asm); find it here.
Howdy, all!
I absolutely love Purescript, but I’ve always felt that the FFI is a little bit inflexible. I often find myself writing one-off single-line foreign functions, such as:
foreign import getByIdAndSetFocus :: String -> Effect Unit
Implemented as:
exports.getByIdAndSetFocus =
id => () => document.getElementById(id).focus();
And used like:
do
myId <- calcId
getByIdAndSetFocus myId
Using the FFI for these one-off cases feels very clunky to me. I need to modify three lines across two files in order to implement what is morally a one-liner. (To be fair, I don’t think the FFI is intended to be used this way, but I am choosing to do so anyway. )
To aid with these cases, I created a tool called ps-inline-asm
to allow for more flexible FFI usage. It supports emedding arbitrary javascript expressions within purescript code, and even interpolation between the two languages.
Using ps-inline-asm
, I could rewrite the above example as just:
do
myId <- calcId
asm " () => document.getElementById(#{myId}).focus() "
Then ps-inline-asm
would compile this into a foreign import and foreign module on my behalf.
I’m already using this tool in one of my projects, and am announcing its existence to the Purescript community in case anybody else finds it useful. It’s not production-ready right now, but I’m happy to continue working on it if people find it useful.
Enjoy!