|
|
|
|
|
by deltaonefour
1673 days ago
|
|
>You would never write an exception handler to handle such a failure from div. The divisor being non-zero is a precondition of calling div in the first place, which is something the caller is responsible for upholding. You shouldn't ever need to write an exception handler to catch precondition violations. Do you also write handlers to 'handle' null dereferences? Representing the partiality in the return type is just pushing the responsibility to some code that can't reasonably do anything. This is just your arbitrary preference. There is nothing wrong with going from either perspective. But your exception is completely worse from every quantifiable metric except for your opinionated qualitative metric. fromInt : Int -> Optional[NonZero[Int]]
This is functions suffers from the same problem you describe. You're just trying to justify a convention of doing this check before rather than later. Also Your unsafe version is again worse because it will trigger an exception on zero, so I don't see how it helps your argument.>But encoding it in the argument type ensures the check is done before div is called which is where it needs to be done. This is the core of your argument and it is highly flawed. There is no "need" for it to be done this way. It is simply your preferred convention. Your argument loses on both fronts. Exceptions are definitively worse and Encoding non zero type safety into the parameter is not necessarily proven to be better. |
|
It's not arbitrary since it's possible to write your function using mine but not vice versa. If you disagree then please implementing the following function without casting:
> You're just trying to justify a convention of doing this check before rather than laterThe convention that callers are responsible for upholding the preconditions of the functions they call is well established: https://en.wikipedia.org/wiki/Design_by_contract. You obviously can't fix precondition violations by checking the result after the fact.
> Also Your unsafe version is again worse because it will trigger an exception on zero
That is the point of the unsafe version, yes. Sometimes you will statically know the argument is non-zero e.g. NonZero(3). If you want to avoid an exception then use the safe version.