develooper Front page | perl.perl5.porters | Postings from March 2023

Re: Pre-RFC Deep constants

Thread Previous | Thread Next
Dave Mitchell
March 13, 2023 12:35
Re: Pre-RFC Deep constants
Message ID:
On Thu, Mar 09, 2023 at 04:58:39PM +0000, Robert Rothenberg wrote:
> I'm a fan of using constants in code, and often using interpolatable
> constants with something like Readonly or Const::Fast for deep constants:

Unless I missed it, there doesn't seem to have been any discussion on
compile time v run time behaviour.

For example in current perl, in this:

    use constant one => 1;
    my $x = one + 2;

the addition gets constant folded at compile time, so it's the same as if
the code was written

    my $x = 3;

What happens for a hypothetical constant lexical var? E.g.:

    my $one :constant = 1;
    my $x = $one + 2;

Does the addition get constant folded? If so, what about:

    my $i = 1;
    my $one :constant = $i;
    my $x = $one + 2;

Is this a compile-time error, or does $one act a bit like a state variable
and gets assigned to only once, or is $one just a normal my variable -
except that its a runtime (or maybe compile-time?) error to modify in the
same scope after the initial assign?`

A walk of a thousand miles begins with a single step...
then continues for another 1,999,999 or so.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About