Okay, so if I'm understanding these comments correctly (which is not
guaranteed since I don't grok typeglbos and I've never touched Prolog or used a "with" statement,
we're back to := just saving keystrokes. It isn't lazily evaluated.
Which just doesn't seem right, I have to be missing something.
So how is $bound := %data{$key} different
from $bound = \%data{$key} aside
from the latter being a reference? (And since we'll have
automatic dereferencing to the expected context, that
shouldn't be a problem, correct?)
I now see that = still slurps, so (@a, @b) := (@b, @a) isn't easily reproduceable, and thus
we have a clear use for :=....but is that really the extent of it?
| [reply] [d/l] [select] |
So how is $bound := %data{$key} different from $bound = \%data{$key}
In the first case, $bound becomes another name
for %data{$key}. In the second case, $bound simply stores a reference to %data{$key}.
That one extra level of indirection may not seem important, but consider what happens later in the code
when we write:
$bound = $nuclear_reactor_shutoff_command;
In the first case, the shutoff command goes into the entry
%data{$key}. In the second case, the reference to %data{$key} is (silently!) replaced by the
shutoff command, and %data{$key} itself is not updated at all.
Which is better? Well, of course, it depends on what you were trying to achieve. But, with the departure of
typeglobs, we need to have some way to do the former, and that way is going to be binding.
And, indeed, as I mentioned in E3, binding has a couple of major advantages over typeglobbing: it's typesafe(r), and bound variables can be lexically scoped.
| [reply] [d/l] [select] |
we're back to := just saving keystrokes
Hey! That's an important achievement. ;-)
But, of course, there are many other uses for binding too (some of which have already been mentioned in this thread).
Here's another that hasn't been mentioned yet. Binding is particularly useful when you have lvalue subroutines/methods and you want to do something complex with the lvalue. Rather than repeatedly calling the subroutine/method:
while (<>) {
$obj.threshold += $_
unless $obj.threshold > $min;
}
you can just bind the lvalue once and use the bound variable instead:
my $thresh := $obj.threshold;
while (<>) {
$thresh += $_
unless $thresh > $min;
}
| [reply] [d/l] [select] |
Is this a use beyond simply saving keystrokes, or did I misunderestimate what you wrote?
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] |
Well first of all it is important that something like :=
exists because we need to translate old Perl 5 code into
Perl 6.
But beyond that, well if you don't know how to use typeglobs
in Perl 5, then you likely won't need to know it in Perl 6.
However that does not mean that its job is lessening how
many keystrokes to use. Rather think of it as selectively
changing which keystrokes to use. Without that ability
you couldn't write something like Exporter. Without
the ability to produce Explorer you
wouldn't be able to easily write something like:
use CGI qw(:standard);
my @parameter_names = param();
So you see it is quite important that the capability be in
the language, even if you will never use it directly. | [reply] [d/l] |