p5h / p5summit-2019

Perl 5 Summit
0 stars 0 forks source link

`const` variables #26

Open leonerd opened 4 years ago

leonerd commented 4 years ago

use constant is an ugly hack that breaks lots of syntax (e.g. here)

Nicer would be:

const $ten = 10;

This would a normal lexical that behaves the same as any other normal lexical, but is subject to constant-folding in the same way that constant subs are now.

A new keyword is proposed, rather than say a :const attribute, because the expression on the righthandside is evaluated at BEGIN time:

say "2";
const $x = say "1"; 
say "3";

(the output is printed in numerical order)

Were this written as my $x :const = say "1" it would be lot more subtle to explain why (and implement) the side-effects happening at BEGIN time.