3 ms·
For Perl use const name => value; Though ... I'm not sure why you'd need a "set" constant, as constants should be immutable, and set once ...
by hpcjoe 5y ago
For Perl
use const name => value;
Though ... I'm not sure why you'd need a "set" constant, as constants should be immutable, and set once ...
- forgotmypw17 5y agoWell, perhaps I should've said SetImmutableVariable? Are consts not allowed to be set at runtime?
- throwanem 5y agoIt sounds like the confusion might be in declaration vs. initialization, which for language-supported consts of course have to happen at the same time. Retrofitting the concept into a language without native support for immutable bindings, you'd of course need some kind of setter on whatever object or similar you're using to encapsulate the binding and ensure it never does actually change.
- forgotmypw17 5y agoI'm combining my lament about lack of "you are using a variable you didn't declare yet" error in PHP and my newly arrived at understanding of the benefits of immutable variables, and then adding to the confusion by using the "constant/const" term, which typically means "set at compile time"
- throwanem 5y agoIn languages with a compilation step, this is true but a side effect. Really, what "const" denotes is an association between a name and a value (ie a binding, in this case an assignment, although there are other kinds of binding such as naming positional arguments in a function signature) which has to be performed when the name is declared, and which cannot be changed once made. (Hence for example in Javascript why properties of an unsealed object can still be modified even via a const binding - what's immutable isn't the value, but the association of that value with the const-declared name.)
- darrenf 5y agoNitpick: use constant name => value; Albeit `Const::Fast` might be considered preferable for several reasons (deeply readonly structures, variables which you can interpolate rather than barewords, etc).
- hpcjoe 5y agoAgreed. I tend to use syntactic sugar like use constant true => (1==1); use constant false=> (1==0); in many of my codes. It lets me use them as barewords, and makes the logic it appears in, saner. But this is a style/taste thing.