5 ms·
When you implement a generic trait that is designed to handle errors but your specific case is infallible then it is helpful to indicate that the error case can
by the8472 8y ago
When you implement a generic trait that is designed to handle errors but your specific case is infallible then it is helpful to indicate that the error case can never occur. Marking that as uninhabited makes it easier for downstream consumers to just eliminate all code related to error handling for that concrete type.
- zebraflask 8y agoPlease don't flame me if this is an ignorant question, but how is uninhabited different from Javascript's undefined? That's what I would use undefined for in JS (very loosely speaking).
- Veedrac 8y agoYou can actually set a variable to `undefined` in Javascript. You can't set a value to an uninhabited type─there is no value that represents the type.
- zebraflask 8y agoI get it, so the concept is closer to declaring a variable and that's it, nothing further happens. Thanks.
- sacado2 8y ago"undefined" is a type that has exactly one possible value. An uninhabited type has no possible value.
- phyzome 8y agoAn example I can think of is Java's StringReader class. It implements the Reader interface, so it has to bear the "throws IOException" signature... but it's impossible for it to produce an I/O error, since it's not actually doing any I/O, just reading from a String. That's fine if you're using it via the Reader interface, but if you're working with StringReader directly it's a pain in the butt to deal with the non-existent exceptions. It sounds like never-patterns would help with that.