Re: RFC 83 (v2) Make constants look like variables
On 8/16/00 12:37 PM, Perl6 RFC Librarian wrote: This RFC proposes that the current constant.pm module removed, and replaced with a syntax allowing any variable to be marked as constant. Unfortunately, I submitted an nearly identical RFC yesterday because I didn't see the existing one (I searched for "const" on the RFC homepage, I swear! :) I'm not sure if mine will end up getting posted, but in case it doesn't, here are my comments/additions to RFC 83: It is proposed that a new syntax for declaring constants be introduced: my $PI : constant = 3.1415926; my @FIB : constant = (1,1,2,3,5,8,13,21); my %ENG_ERRORS : constant = (E_UNDEF='undefined', E_FAILED='failed'); Constants can be lexically or globally scoped (or any other new scoping level yet to be defined). I'm not crazy about the "attribute" syntax's use for constants. I prefer this: constant $PI = 3.1415926; which nicely cleans up the my/our situation if you're not opposed to chaining operators: our constant $PI = 3.1415926; if($urban_legend) { my constant $PI = 3.0; ... } If an array or hash is marked constant, it cannot be assigned to, and its elements can not be assigned to: @FIB = (1,2,3); # Compile time error @FIB[0] = 2; # Compile time error %ENG_ERRORS=(); # Compile time error %ENG_ERRORS{E_UNDEF='No problem'} # Compile time error I also extended this to ensure that nothing but reading can be done to constant collections. So this is also true: $#FIB = 100; # Compile-time error (Not sure if that was implied in the examples given.) To create a reference to a constant use the reference operator: my $ref_pi = \$PI; To create a constant reference use a reference operator in the declaration: my $a = 'Nothing to declare'; my $const_ref : constant = \$a; I didn't address references, but I figured they would look like this: my $a = 'Nothing to declare'; my constant $ref = \$a; EXTENSIONS It may be desirable to have a way to remove constness from a value. This will not be covered in this RFC--if it is required a separate RFC should be written referencing this one. I didn't address this issue either, but my feeling is that it definitely should NOT be possible to "remove const-ness" from a variable. Part of the motivation for my constants RFC was the ability to do clever constant folding at compile-time, even going so far as to fold this: print $obj-method_returning_constant(); into the run-time equivalent of printing a constant string, when possible. (A bit more detail is in my RFC, which, of course, is "in my other computer" at the moment...and/or lost in the ether on its way to the RFC address. I'd post the relevant section if I had it.) -John
Re: RFC 83 (v2) Make constants look like variables
On 8/16/00 3:55 PM, John Siracusa wrote: On 8/16/00 12:37 PM, Perl6 RFC Librarian wrote: This RFC proposes that the current constant.pm module removed, and replaced with a syntax allowing any variable to be marked as constant. Unfortunately, I submitted an nearly identical RFC yesterday because I didn't see the existing one (I searched for "const" on the RFC homepage, I swear! :) I'm not sure if mine will end up getting posted... ...and three messages later it appears. Typical. Anyway, sorry if it's redundant. You can take a look for yourself: RFC 113 -John
Re: RFC 83 (v2) Make constants look like variables
"JS" == John Siracusa [EMAIL PROTECTED] writes: JS On 8/16/00 12:37 PM, Perl6 RFC Librarian wrote: It is proposed that a new syntax for declaring constants be introduced: my $PI : constant = 3.1415926; my @FIB : constant = (1,1,2,3,5,8,13,21); my %ENG_ERRORS : constant = (E_UNDEF='undefined', E_FAILED='failed'); Constants can be lexically or globally scoped (or any other new scoping level yet to be defined). JS I'm not crazy about the "attribute" syntax's use for constants. I prefer JS this: JS constant $PI = 3.1415926; Propose it but I believe that Larry has already semi-blessed the attribute version. The problem with the prefix version is that that is already reserved for my/our/local. And constant-ness in my mind is an attribute or modifer of the value or perhaps of the container. It has no bearing on the scoping or type of the value. chaim -- Chaim FrenkelNonlinear Knowledge, Inc. [EMAIL PROTECTED] +1-718-236-0183
RFC 83 (v2) Make constants look like variables
This and other RFCs are available on the web at http://dev.perl.org/rfc/ =head1 TITLE Make constants look like variables =head1 VERSION Maintainer: Jeremy Howard [EMAIL PROTECTED] Created: 10 August 2000 Last modified: 16 August 2000 Version: 2 Mailing List: [EMAIL PROTECTED] Number: 83 =head1 ABSTRACT This RFC proposes that the current constant.pm module removed, and replaced with a syntax allowing any variable to be marked as constant. =head1 CHANGES =head2 Since v1 =over 4 =item * Changed notation to be consistent with other value attributes =item * Specified behaviour of references, arrays, and lists =item * Added definition =back =head1 DESCRIPTION A constant is a value that can not be changed once it is declared. Once declared, it behaves just as if it was the actual literal value that it contains. Currently, constants are created in Perl using the constant.pm module: use constant PI = 3.1415926; which creates an inlined subroutine: sub PI () {3.1415926;} This method of creating constants has three serious drawbacks: =over 8 =item Can not be interpolated in strings Whereas variables can be interpolated into strings (e.g. "PI is $Pi"), subroutines can not be. This makes using constants inconvenient, since string concatenation must be used. =item Inconsistant syntax The sudden appearance of barewords can be quite unsettling to new users. After becoming told that 'arrays are @name, scalars are $name, ...', the rule suddenly stops working just because the programmer wants the value to stay constant. =item Redundant warnings In persistant Perl environments such as mod_perl, inlined subroutines often created the redundant warning 'Constant subroutine PI redefined'. This has been a frequent source of confusion amongst new mod_perl users. =back It is proposed that a new syntax for declaring constants be introduced: my $PI : constant = 3.1415926; my @FIB : constant = (1,1,2,3,5,8,13,21); my %ENG_ERRORS : constant = (E_UNDEF='undefined', E_FAILED='failed'); Constants can be lexically or globally scoped (or any other new scoping level yet to be defined). If an array or hash is marked constant, it cannot be assigned to, and its elements can not be assigned to: @FIB = (1,2,3); # Compile time error @FIB[0] = 2; # Compile time error %ENG_ERRORS=(); # Compile time error %ENG_ERRORS{E_UNDEF='No problem'} # Compile time error To create a reference to a constant use the reference operator: my $ref_pi = \$PI; To create a constant reference use a reference operator in the declaration: my $a = 'Nothing to declare'; my $const_ref : constant = \$a; Note that this does not make the scalar referenced become constant: $$const_ref = 'Jewellery'; # No problems $const_ref = \4; # Compile time error =head1 IMPLEMENTATION Constants should have the same behaviour as the do now. They should be inlined, and constant expressions should be calculated at compile time. =head1 EXTENSIONS It may be desirable to have a way to remove constness from a value. This will not be covered in this RFC--if it is required a separate RFC should be written referencing this one. =head1 REFERENCES perldoc constant perldoc perlsub (for constant subroutines)