Patrick R. Michaud wrote:
>"store in a data structure"  -->  "bind using :="

That's not going to pass well through anything that treats the data
as values.  For example, after your

>> my @c;  @c[2] := $a;  $a = 5;  say @c[2];
>5

if I then do

        my @d = @c.map({ $_ })

then @d[2] is not an alias of @c[2] or $a.  It's also quite inconvenient
to bind into a data structure like that.  The actual code I'm working
on at the moment has the form

        my $flange_width;
        my $farthingale_height;
        my @computations = (
                { output_loc => $flange_width.VAR, formula => "aaa" },
                { output_loc => $farthingale_height.VAR, formula => "bbb" },
        );
        loop {
                # ...
                for @computations {
                        # some computation using $_<formula>
                        if $got_it { write_to($_<output_loc>, $result); }
                }
        }
        # later code uses $flange_width and $farthingale_height

There are multiple computations to perform, which are done by similar
processes, and they benefit from partial computations being interleaved.
So they benefit from the partial computations being described by an
array that's looped over.  But once it's done, the final outputs are
treated quite individually.

To do this with := means that I can't create @computations in a neat table
like that, with each entry self-contained.  I'd have to do something like

        my @computations = (
                { formula => "aaa" },
                { formula => "bbb" },
        );
        @computations[0]<output> := $flange_width;
        @computations[1]<output> := $farthingale_height;

Note that related items (output location and formula, for a particular
computation) are now separated.  The .VAR version had the nice feature
that the reference, being an ordinary value, can be mentioned inline in
the constructor expression.

I suppose I could add an extra level of data structure: create a
one-element array and bind my target into it, then use that array as my
valueish reference.  The array can be passed around by value, put into
a data structure, and so on.  Something like

        sub ref_to(Mu $a is rw) { my @s; @s[0] := $a; @s }
        sub read_ref(@s) { @s[0] }
        sub write_ref(@s, Mu $nv) { @s[0] = $nv }
        my $a = 3;
        my $b = ref_to($a);
        write_ref($b, 5);

But that seems perverse, when the Scalar object itself is visible to
the language.  In principle the Scalar object is exactly what I'm after;
it's just missing a write method.

>More generally -- when I last checked Perl 6 doesn't really have
>"references" in the same style as Perl 5

I'm not particularly looking for the same style.  I'm looking for the
basic ability.  In Perl 5 it's done by reference scalars, in C it's done
by pointers; it is surprising that Perl 6 would not support this paradigm.

>magically transparent when you need them to be

I'm not clear what you're referring to here.  The way I interpret those
words, Perl 5 references are never magically transparent: there is
always an explicit distinction between the reference and the referent.
(Except for the now-removed experimental autoderef feature on a
few builtins.)  Perl 6, on the other hand, frequently has operations
silently pass through a container.  (Named method calls on the Scalar,
where not handled by the Scalar class, used to be passed through to the
contained value, but that seems to have stopped working.)

-zefram

Reply via email to