OvermindDL1 / bucklescript-tea

TEA for Bucklescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Runtime error by passing a function to an onClick

tcoopman opened this issue · comments

I'm not sure if this is the best way to use TEA, but I've run into a runtime error in this situation:

type msg = Foo of (int * msg Tea.Cmd.t)

let msgs id = Tea.Cmd.call (fun callbacks -> ....)

let view =
  div [onClick (Foo (1, (msgs 1)))] []

I get an error on

let compareEventHandlerTypes left = function

because this uses ocaml.equal which doesn't like comparing functions:

var b_type = typeof b;
        if (a_type === "function" || b_type === "function") {
          throw [
                invalid_argument,
                "equal: functional value"

Doesn't Elm cause a runtime error as well in the same case? I know passing functions via messages is not 'safe' in the Elm style (nor storing them in the model).

I wonder if there is a way to turn this into a compile-time error, at the very least I will leave this open to research in to doing that (no method is popping immediately to mind)...

One of the guarantees of Elm is that you will not see runtime errors in practice
-- https://guide.elm-lang.org/error_handling/

It uses immutability, effects and exhaustive check to do the trick.

It uses immutability, effects and exhaustive check to do the trick.

It fails at the 'will not see runtime errors in practice' quite a bit. There are a LOT of cases it will crash at runtime.

For note, OCaml actually does quite substantially more exhaustiveness checks than Elm with a far better type system (One good way to crash the runtime involves some number tricks that have occured in practice). Plus also immutability there.

And Elm's effects are... more like message passing processes, not effects.

There are a LOT of cases it will crash at runtime.

You are so right

Number 3 — function equality
-- https://medium.com/@eeue56/top-6-ways-to-make-your-elm-app-crash-at-runtime-562b2fa92d70

Heh, yeah I hit quite a number of them with fairly stock code when I was developing an Elm app (not even javascript related crashed, I know how to deal with the DOM), that combined with the hostile community steered me away from it, but it was still an interesting design that I wanted to pursue, however I had a lot of code that I'd have to rewrite, instead of doing that I made this bucklescript-tea library and did very minor edits to get it working with bucklescript instead and I'm using that now. :-)