Hacker News new | ask | show | jobs
by jfmengels1 1966 days ago
Elm doesn't have asserts. Instead, what you'd do is to have the function return a potential error (what in Elm we'd call a Result).

so instead of

function a(b) { assert(b > 0); return b + 1; }

you'd do

a b = if b > 0 then Ok (b + 1) else Err "b was not > 0"

To then use the result of the function call, you'll need to unwrap the result, by handling the case where the function returned Ok, but also the case where you return Err

case a -2 of Ok b -> -- display the value b Err errorMessage -> -- display the error message

THis is the kind of technique Elm uses to ensure that you'll have no runtime errors in your code: by forcing you to handle the error cases. The nice thing is that the types can indicate whether something can fail or can't.

1 comments

Asserts are not for runtime errors, but for verification of assumptions in debug builds. Essentially, they are mini-test run on real data during normal use (of a debug build).

Making it so you can't have asserts will just mean that you won't have those kind of tests.

Ah alright. That's an interesting technique! You can't do that in Elm, you'd have to rely on the technique I mentioned above and regular tests.