Hacker News new | ask | show | jobs
by the__alchemist 1255 days ago
I concur. I would use an `Option`, `Result`, or enum for all of those scenarios, depending on the details.
1 comments

No way. It'd be ridiculously slow to constantly check for NaN let it propagate and then use a result or option at higher level.

Adding a branch like that to low level number crunching would be bonkers.

Indexing an array isn't low level number crunching, and whatever you produce from that is going to be the result of a branch unless you jist don't boubds check arrays ans return some random bit of memory (or crash because you indexed out of program memory.)

Honestly, ideally you have dependent types and index out of the array is a type error caught at compile time, but where its not, “NaN” is not the most logical result, even for floats, because very often you will want to distinguish “the index given was out of the array” from “the index given was in the array and the value stored was a NaN”; special values IN the domain of the values stored in the array are fundamentally problematic for that reason.