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

Re: Pre-RFC Deep constants

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
March 13, 2023 12:35
Subject:
Re: Pre-RFC Deep constants
Message ID:
ZA8YgCf6A1dcc3nK@iabyn.com
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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About