News for hackers. Stuff that matters.
Re:Safe dead code removal in a pure functional language by jfmengels1 on Friday January 29, 2021 @02:23AM (#25954937)
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.

Re:Safe dead code removal in a pure functional language by account42 on Friday January 29, 2021 @08:12AM (#25956885)
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.

Re:Safe dead code removal in a pure functional language by jfmengels1 on Friday January 29, 2021 @10:36AM (#25958302)
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.