bucklescript-tea icon indicating copy to clipboard operation
bucklescript-tea copied to clipboard

Runtime error by passing a function to an onClick

Open tcoopman opened this issue 8 years ago • 5 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 https://github.com/OvermindDL1/bucklescript-tea/blob/51403a6242a35bf0e735fb3deccc5439e56a10cc/src/vdom.ml#L154

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"

tcoopman avatar Jun 18 '17 15:06 tcoopman

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)...

OvermindDL1 avatar Jun 19 '17 16:06 OvermindDL1

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.

stereobooster avatar Feb 21 '18 21:02 stereobooster

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.

OvermindDL1 avatar Feb 21 '18 22:02 OvermindDL1

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

stereobooster avatar Feb 21 '18 22:02 stereobooster

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. :-)

OvermindDL1 avatar Feb 21 '18 23:02 OvermindDL1