I used "dualvar" also, in MCE::Shared::Cache. When max_age is specified during construction of the cache, the dualvar is used internally to store the expiration time with the key name in the keys array.
# update expiration
$keys->[ $off ] = ( $exptime >= 0 )
? dualvar( time + $exptime, $_[1] )
: dualvar( -1, $_[1] );
Basically, the "dualvar" capability in Perl allows the "max_age" feature without increasing the memory consumption of the cache or impacting performance. | [reply] [d/l] |
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)
| [reply] [d/l] [select] |
> Save it for very special situations, like impressing people at conferences and cocktail parties.
The use of "dualvar" in MCE::Shared::Cache is not due to schizophrenic behavior or trying to impress people. Nothing like that.
There was a dated article on the web (I misplaced the link) where someone compared several pure-Perl caching modules. In addition to performance, the author of the article compared memory consumption. The thing that I remember is the extra memory consumption using a hash to store the expiration time.
For me, "dualvar" solved a problem. I was able to add the "max_age" feature to MCE::Shared::Cache without greatly impacting performance or spike in memory consumption.
| [reply] |