Poor man's static exception analysis with alerts

This reminds me of E’s escape. Found this example with a bit of grepping:

Essentially, this defines cantDelete as a local exception and then passes it to each.delete (e2 is the exception’s payload - a message saying why it failed).

1 Like

I’m a bit late to the party here but I just wanted to note that we can easily turn these alerts into compile errors at the (file) module level with the ++ syntax.

Wasn’t obvious to me, and it requires no configuration so that’s quite ideal with a dune workflow.

I also had to fiddle a bit to find the proper syntax for implementation-only code (no inline module, no mli file).

So adding these alerts is pretty low friction, very cool idea.

[@@@alert "++crash"]

let (foo [@alert crash]) =
  fun n ->
  if n = 0 then
    failwith "Bad number"
  else
    n
;;

let () =
  Printf.printf
    "Foo is: %d\n"
    begin
      try foo 0 [@alert "-crash"] with
      | _ -> -1
    end
;;
2 Likes