4 ms·
As a Elm non-user, naive question: would it be possible to use the JS interop (ports) for this, at cost of some clunkiness?
by fulafel 3y ago
As a Elm non-user, naive question: would it be possible to use the JS interop (ports) for this, at cost of some clunkiness?
- lolc 3y agoYes the answer is always ports when this topic comes up. Unfortunately one can only pass some basic types through ports. Passing a RegExp or an Intl.DateTimeFormat is not possible. It needs a wrapper on the Elm side, and the Elm people decided I can't be trusted to write this wrapper for me. Back then if I wanted to search a list of strings for entries that match a regex supplied at run-time, I'd have to pass the whole list through a port, filter it outside, then pass it back in. Rather than just using the filter function. Ports are asynchronous messaging which means I have to restructure the whole code and wait for a state change when the filtered list is returned. Let me cite the Elm docs on ports[1]: "Definitely do not try to make a port for every JS function you need." So! Where does that leave me? Unsupported, that's where. Because I need a JS function. In 0.18 unsupported was fine. They broke it for 0.19 and the project died. Maybe it was dying of other causes anyway, but that one action sure drove people away. [1] https://guide.elm-lang.org/interop/ports https://guide.elm-lang.org/interop/ports
- marc136 3y agoYes, ports or custom elements are the recommended options, https://github.com/elm-community/js-integration-examples https://github.com/elm-community/js-integration-examples There are a bunch of other options/workarounds/hacks depending on the need. E.g. using getters or creating proxy objects https://github.com/anmolitor/intl-proxy https://github.com/anmolitor/intl-proxy, or event listeners, or postprocessing of the generated JS code, but those shouldn't be the first idea to reach for.