I really like the idea of constants in perl, but think the RFC should
go a lot further.  C/C++ has solved this problem; we should follow in
their footsteps.  Spinning off from Larrys syntactic comment and Mike
Pastores example, how about some of the following:


A constant struct with constant values:

    my %jane : const = (
         Name =>    'Jane',
         Age =>     30,
         Gender =>  'Female'
         Kids =>    2.5
    ) : const ;

    %jane = %janet;             # Not allowed
    %jane{Kids}++;              # Not allowed
    %jane{Husband} = \%bob;     # Not allowed


A non-constant struct with fixed members and constant values:

    my %jane = (
         Name =>    'Jane',
         Age =>     30,
         Gender =>  'Female'
         Kids =>    2.5
    ) : const ;

    %jane = %janet;             # Not allowed
    %jane{Kids}++;              # Not allowed
    %jane{Husband} = \%bob;     # Not allowed
    %jane = %janet;             # Allowed


A constant struct with unremovable members, extensions allowed to
members, and variable values:

    my %jane : const = (
         Name : const =>    'Jane',
         Age : const =>     30,
         Gender : const =>  'Female'
         Kids : const =>    2.5
    ) ;

    %jane = %janet;             # Not allowed
    %jane{Kids}++;              # Allowed
    $jane{Husband} = \%bob;     # Allowed
    delete $jane{Husband};      # Allowed
    delete $jane{Kids};         # Not allowed


A constant struct with a mix of removable and unremovable members
and a mix of constant and variable values:

    my %jane : const = (
         Name : const =>    'Jane',
         Age : const =>     30 : const,
         Gender : const =>  'Female'
         Kids =>            2.5
    ) ;

    %jane = %janet;             # Not allowed
    %jane{Kids}++;              # Allowed
    %jane{Age}++;               # Not allowed
    $jane{Husband} = \%bob;     # Allowed
    delete $jane{Husband};      # Allowed
    delete $jane{Kids};         # Allowed


It would also be useful to have a mostly constant item that one
or two changes are allowed in with a `var' antonym for `const':

    my %jane : const = (
         Name =>    'Jane',
         Age =>     30 : var,
         Gender =>  'Female'
         Kids =>    2.5
    ) : const ;

    %jane = %janet;             # Not allowed
    %jane{Kids}++;              # Not allowed
    %jane{Kids}++;              # Allowed
    $jane{Husband} = \%bob;     # Not allowed
    delete $jane{Husband};      # Not allowed
    delete $jane{Kids};         # Not allowed


This could also be used to allow the hash to be extended:

    my %jane : const = (
         Name =>    'Jane',
         Age =>     30 : var,
         Gender =>  'Female'
         Kids =>    2.5,
         : var =>
    ) : const ;

    %jane = %janet;                     # Not allowed
    %jane{Age}++;                       # Not allowed
    %jane{Kids}++;                      # Allowed
    $jane{Husband} = \%bob;             # Allowed
    delete $jane{Husband};              # Allowed
    delete $jane{Kids};                 # Not allowed

The programmer should be able to use `const' and `var' in element
creation and modification as well.  Using the same structure
as in the previous example:

    my %jane = (
         Name =>    'Jane',
         Age =>     30 : var,
         Gender =>  'Female'
         Kids =>    2.5,
         : var =>
    ) : const ;

    $jane{Husband} = \%bob;                     # Allowed
    delete $jane{Husband};                      # Allowed
    $jane{Husband : const} = \%dave;            # Allowed
    delete $jane{Husband};                      # Now not allowed
    $jane{Husband : const} = \%jeff : const;    # Allowed
    delete $jane{Husband};                      # Now not allowed
    $jane{Husband : const} = \%bill;            # Now not allowed

    $jane{Religion} = "Catholic" : const;       # Allowed
    $jane{Religion} = "Protestant";             # Not allowed
    delete $jane{Religion};                     # Allowed
    $jane{Religion} = "Protestant" : const;     # Now allowed
    $jane{Religion} = "Catholic";               # Not allowed
    delete $jane{Religion};                     # Allowed
    $jane{Religion : const};                    # Allowed
    delete $jane{Religion};                     # Now not allowed
    $jane{Religion} = "Protestant";             # Allowed
    $jane{Religion} = "Catholic" : const;       # Allowed
    $jane{Religion} = "Protestant";             # Now not allowed

The same sort of thought process should be applied to lists, arrays,
references, and objects.  IMHO the RFC should be updated to reflect
all these issues before we can properly consider it.  Sorry to rain
all over your parade, Jeremy.  The core idea is great, but the
implementation section really needs expansion.

And of course, the above only reflects my opinion.

Reply via email to