Recently $larry asked for ideas for better naming 
the several states of write access.
There are some tentative thoughts, I like to offer.

Larry Wall wrote:
> That being said, in writing the Perl 6 grammar I keep running into the
> need for rw context variables.  I'm getting tired of writing things 
> like:
> 
>     my @heredoc_stubs is context is rw = ()
> 
> so maybe there's some general syntactic relief for rw/ro that is
> orthogonal to everything else.  But that means it wouldn't be a
> trait, a type, a sigil, or a twigil, if it's really orthogonal.
> I don't just want to go with bare rw and ro markers because I think
> they're too easy to confuse.  It's more like we want an initializer
> that says "lock this after I change it".  If used on the initializer
> of a declaration it would have the force of a readonly declaration.
> Some variant on or near the =, in other words, that could also be
> a variant on normal assignment.  But maybe it also has to
> stand alone to declare it as a "write once" variable, I expect.
> Currently "is readonly" only works on the declaration, and only on
> items that are initialized by the declaration.  Maybe that's good
> enough for most purposes.

My approach is:
In terms of writeability a variable/reference can be
(1) a variable, (2) a constant or (3) a "final" 
(Don't wonder if you never heard of that "final", just my proposal)
That means it can be writeable (1), 
not writeable since compile time (2) or
not writeable since that moment in runtime when it gets "final" (3).

This approach could work with an optional second declarator.
In the usual case the second declarator, that would be 'variable',
is simply omitted and nobody feels a difference.

--------- snip
my $number = 3;          # OK, of course
my variable $number = 3; # The same, not really useful, -> warning?

# context variables default to readonly, 
# so the 'variable' declaration has an effect
my variable @heredoc_stubs is context = (); # Writeable context!

# Compile time constant with local scope:
my constant $pi = 3.14;

# Package scoped constants can be useful!
say $Math::euler; # Prints constant value from package Math

# This data from database has to be 'readonly':
my final $temperature = db_temperature_of( $date_time_loc);

# But this is possible as well:
my $temperature = db_temperature_of( $date_time_loc);
if is_fine($temperature) {
    final $temperature;
}
else {
    $temperature = db_default_temperature();
    final $temperature;
}
--------- /snip

I think this approach is quite expressive, readable and orthogonal 
to everything else. In particular it is orthogonal to the scope. 

Whenever you see the 'variable' declarator, you should think: 
Attention, it is really variable.

With the "final" declarator you can lock a variable at 
initialization and with the "final" build-in multi sub, you can 
lock it at any time.

There may be better words than "final". I just thought, it matches 
the meaning to some extend, its short and it can be used as a 
verb and as a noun.

The approach is even extensible. I could imagine an otional 
declarator for predeclared variables that requires that this 
variables will really become defined.

Kind Regards
 Stefan


 
---------------------------------
Need a quick answer? Get one in minutes from people who know. Ask your question 
on Yahoo! Answers.

Reply via email to