4 ms·
I don't think this really solves the problem. It just kicks the same problem over to addition instead. let a = 17 let b = 10 let c = 7 let d =
by mywittyname 8y ago
I don't think this really solves the problem. It just kicks the same problem over to addition instead.
let a = 17
let b = 10
let c = 7
let d = a - (b + c) // throws AddsToZeroException
> Make division by zero impossible with the type-checker.
Forcing programmers to consider division by zero when it occurs can be done simply by having the language compiler force handling of a divide by zero exception when division occurs.
- spooneybarger 8y ago> Forcing programmers to consider division by zero when it occurs can be done simply by having the language compiler force handling of a divide by zero exception when division occurs. Which is what Pony originally did and, lordy that can be an ergonomic nightmare when I know I don't have 0 and can prove it except, I can't prove it to the compiler and the type system. This problem is why integer division by zero is zero in Pony. Because, I can as a programmer prove that I won't divide by zero, but having a type system or compiler where I can prove it to it that is a computation engine that takes user input is a very non-trivial problem. The solution in Pony is going to be a combination of "safe math operators" and adding value dependent typing to the compiler. "safe math operations" would be: *?, /?, -?, +? that will error on division by zero as well as integer overflow and underflow (and have slower performance as well)
- gowld 8y agoWouldn't it be better to have an explicit unsafeCastToNonZero() function in the stdlib instead forcing invisible unsafe casts in every division statement of every program?
- dllthomas 8y ago> Forcing programmers to consider division by zero when it occurs can be done simply by having the language compiler force handling of a divide by zero exception when division occurs. That's materially different than a non-zero type, and IMO it's meaningfully worse. The type checker is most useful when it moves information.