Relief for rw/ro

2 views
Skip to first unread message

Steve Lukas

unread,
Feb 21, 2007, 3:10:12 PM2/21/07
to perl6-language
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.

Steve Lukas

unread,
Feb 22, 2007, 7:15:07 AM2/22/07
to perl6-language
PS:
In between, I think 'variable' is too long, so:
$code =~ s/variable/vari/g;

IMHO C<vari> is better than C<var> because
C<var> doesn't look like a special thing, but it is.

I feel that the most usual cases for read/write
would be better readable with that approach.
Instead of
C<$res=funcy($foo is rw);> it would be
C<$res=funcy(vari $foo);>

And instead of
C<has $.value is rw;> it would be
C<has vari $.value;>

The question is probably if the writeability is a trait
like other traits and should therefore be written
in the C<is ...> form or is it so special, that a
second delararator is appropriate.
The second declararator has the advantage that
the very important information of exceptional
write-protection or exceptional write-access stands
on the left side, as it should be.

Kind Regards
Stefan



---------------------------------
Sucker-punch spam with award-winning protection.
Try the free Yahoo! Mail Beta.

Dr.Ruud

unread,
Feb 22, 2007, 2:57:58 PM2/22/07
to perl6-l...@perl.org
Steve Lukas schreef:

> In between, I think 'variable' is too long, so:
> $code =~ s/variable/vari/g;

I don't think it is too long, since most of the times you don't need to
mention it. Could 'rw' be an alias?

my rw @heredoc_stubs is context = ();

my @heredoc_stubs is rw context = ();

my @heredoc_stubs is context.rw = ();

my @heredoc_stubs is context/rw = ();

my @heredoc_stubs is context+w = ();

--
Affijn, Ruud

"Gewoon is een tijger."

Reply all
Reply to author
Forward
0 new messages