Interesting!
In computer programming, a semipredicate problem occurs when a subroutine intended to return a
useful value can fail, but the signalling of failure uses an otherwise valid return value.
The problem is that the caller of the subroutine cannot tell what the result means in this case.
The division operation yields a real number, but fails when the divisor is zero.
If we were to write a function that performs division, we might choose to return 0 on this invalid input.
However, if the dividend is 0, the result is 0 too.
This means there is no number we can return to uniquely signal attempted division by zero,
since all real numbers are in the range of division.
-- from Semipredicate problem (wikipedia)
Vaguely related is C++-17's std::variant
and Haskell's Type Inference system, with types like Maybe Real and Either String Char.
So perhaps we can say that Perl's dualvar
helps solve the Semipredicate problem ...
though admittedly, without
dualvar, ordinary Perl
scalars can use undef to signal invalid input.
I'm not an expert on any of this, just coincidentally happened to be looking at this topic
while updating my notes on exception handling vs error returns in functions. :)
Ideas/corrections and other cool references on this topic welcome!
References Added Later
- Why do we need Monads? (SO) - describes rationale for Maybe Real in Haskell, namely to signal errors in a language using functions only (i.e. without Exception handling), where their solution is to allow functions to return two kinds of things
- Result type (wikipedia) - in Haskell, Either type; in C++, std::expected<T, E>; in Rust, enum Result<T, E> { Ok(T), Err(E) }
- Hash value test of zero (mentions Larry's infamous "0 but true" and "0E0", which evaluate to 0 as a number, but true as a boolean)
- Re^3: Rosetta Code: Long List is Long (Updated Solutions - dualvar) (example using dualvar in the long-running Long List is Long saga)
- Re: eval to replace die? (Exceptions and Error Handling References)
- std::error_code (cppreference.com, since C++11) - a platform-dependent error code
- std::expected (cppreference.com, since C++23) - an object of std::expected<T,E> holds an expected value of type T, or an unexpected value of type E; it's never valueless
- std::unexpected (cppreference.com, since C++23)
- Expect the expected (youtube) by Andrei Alexandrescu
- std::variant (cppreference.com, since C++17) - a type-safe union; you can avoid exceptions by having a function return, for example, a std::variant<string, error_code> (similar in style to Haskell) - see Stroustrup, A Tour of C++, p 209 (superseded by std::expected?)
- std::optional (cppreference.com, since C++17 - manages an optional contained value, i.e. a value that may or may not be present)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.