Runtime Error without Executing Fuction!

I love elm! I’ve found it very fluid to develop with.

I have just noticed however that % makes it possible to create invalid code! ( and also a runtime exception)

If I define this function I can get compiled, but non-working code bundle, even though I do not execute this function anywhere!

– this will divide by zero when it is defined
unsafeFun =
start = 0
end = 3
len = List.length []
List.range start end |> (\idx -> idx % len)

very interesting !

Best ,

It is because your ‘unsafeFun’ is not a function, it is a constant. Constants are evaluated at start up time, and your constant definition contains a divide by zero.

I am surprised though as ‘/ 0’ in Elm evaluates to zero, not a runtime exception. Does this not hold true of ‘% 0’?

Correct; at the moment % 0 is an intentional runtime exception (

Since modBy : Int -> Int -> Int, I don’t really agree with the proposal in that issue of returning NaN since that’s a Float. Perhaps a Result String Int is possible, but I’m not sure if people would really appreciate dealing with a Result when doing arithmetic.

Aren’t we striving for total functions? There are some inputs that cannot be mapped to the output. We tend to use Maybe or Result to expand the output set in a way that covers all inputs of that typed.

I think that it should be that the behavior of / 0 and % 0 is consistent. So if the approach to avoid runtime exceptions is to do math wrong, then both should always return 0.

Its not easy to reconcile infinities with integer math and no runtime exceptions; something in that triangle has to give. However, once it is decided that ‘no runtimes’ is the top priority then applying that would yield a consistent approach at least.

I wonder, is this stuff actually going to get fixed in 0.19? Strikes me that Elms math issues could be tabulated on a single page, a consistent approach to fixing them decided upon, and open them up to contributors submitting PRs to fix them. “Code isn’t the problem” for Elm, but I think in this case there is an hour of thinking and deciding to be done, and lots of little bits of coding that is not particularly challenging or fun but that the community would certainly get done.

Anyway, hope yous all have a good Christmas holidays.

It is already being done here and here.