Front page | perl.perl5.porters |
Postings from March 2023
Pre-RFC Deep constants
Thread Next
From:
Robert Rothenberg
Date:
March 9, 2023 16:58
Subject:
Pre-RFC Deep constants
Message ID:
2d06bc70-9b8d-f3e0-7270-ec61791af1a4@cpan.org
I'm a fan of using constants in code, and often using interpolatable
constants with something like Readonly or Const::Fast for deep constants:
But it would be nice to have a way to specify deep constants without
having to use one of several non-core modules, perhaps allowing the
compiler to optimize code knowing that certain things are constants.
It would also be nice to create state variable constants without
restoring to weird hackery to check if the state is defined, e.g. the
following fails:
use Const::Fast;
for (1..2) {
const state $var => { a => "something", b => 2 };
}
I'm still thinking about what the better syntax is. Some ideas,
1. A constant keyword akin to Const::Fast,
const my $var = ....
This has the advantage of letting devs specify the lexical scope the
variable, e.g. "our", "my" or "state".
2. An attribute
my $var :readonly = ....
3. A special function
my $var = readonly( ... )
There are some other questions:
- Do we want to have separate keywords or options for deep vs shallow
constants?
- Do we want the ability to have write-once values?
- How do we handle blessed references? Do we skip them silently? Throw
an exception? Or mark them as read only?
Thread Next
-
Pre-RFC Deep constants
by Robert Rothenberg