in reply to Re: Comprehending Binary :=
in thread Comprehending Binary :=

I think not.

Larry Wall's use of the word "unification" gave me similar hopes. However TheDamian's comment at Re: Re (tilly) 1: Apocalypse 3 put me straight, and then his comparison with typeglob aliasing made it obvious.

Perl 5 has typeglobs. Perl 6 will not, but needs an equivalent way to do the same thing. := is the answer that they came up with.

But since it is aliasing if you, for instance, try:

$y := $x - 5;
what will happen is that $x - 5 is calculated, that goes into a new (constant) variable, and then $y is aliased to that constant. If you later change $x, $y is still aliased to the constant.

Sorry.

Replies are listed 'Best First'.
Re: Re (tilly) 2: Comprehending Binary :=
by swiftone (Curate) on Oct 06, 2001 at 21:50 UTC
    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?

      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.

      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; }
        Is this a use beyond simply saving keystrokes, or did I misunderestimate what you wrote?
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
      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.