Gordon Henriksen wrote:
>
> Taking a thread from Perl 6 Internals. Will Perl 6 support this behavior?
>
> $ perl <<'EOT'
> my @ary;
> my $ref = \$ary[0];
> $$ref = "value";
> print '$ary[0] : ', $ary[0], "\n";
> EOT
> $ary[0] : value
>
> Presumably the Perl 6 would be:
>
> my @ary;
> my $ref = [EMAIL PROTECTED];
> $$ref = "value";
> print '@ary[0] : ', @ary[0], "\n"; # -> @ary[0] : value
>
> If that's supported, then how will this behave?
>
> my @cows of Cow;
> my $slot = [EMAIL PROTECTED];
> $$slot = new Dog;
Well, Perl6 can infer (at compile time) that $slot is a reference to a
Cow, and from that, it can infer that $$slot may only contain a Cow.
Thus, it should be catchable at compile time.
OTOH, the following is not catchable at compile time:
my @cows of Cow;
my @dogs of Dog;
my $ref = int(rand(2)) ? [EMAIL PROTECTED] : [EMAIL PROTECTED];
$$ref = new Dog;
All that Perl6 can infer at compile time about $ref is that it is a
reference to any(Cow, Dog). So, the assignment is legal at compile
time.
Thus, *some* sort of runtime check needs to be made.
I suppose that instead of $ref being a PerlRef (or however we manage
references), it would be a PerlTypeConstrainedRef, which throws an
exception if you try to store a value the wrong type into it.
> Do we wind up with Clarus or an exception? (Never mind whether you take
> exception with Clarus.)
Clarus would be if we had an object whose type was all(Cow, Dog), eh?:)
> Hopefully an exception, from a user's POV.
An exception at compile time the code you presented, an exception at run
time for the code I presented.
> And this?
>
> my @cows of Cow:
> my @cats of Cat;
> my $ref = [EMAIL PROTECTED];
Ok.
> @cats[0] := $$ref;
Ok, although this is mostly a no-op.
> @cats[1] := @cows[0]; # Just to hammer the point home....
A compile time error.
> $$ref = new Dog;
A compile time error due to type inferencing.
(If type inferencing were insufficient, it would be a run-time error).
> But then there's a question for p6i as to how all the above happens.
I suppose that a PerlRef would be a PMC which has another PMC stored in
it's ->cache.struct_value component; storing and fetching it's contents
would be done via VTABLE_set_pmc and VTABLE_get_pmc.
I suppose that a PerlTypeConstrainedRef would, in addition, have a
PerlClass PMC in it's PMC_data, and check for the appropriate isa
relationship before allowing new data to be stored into it.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "[EMAIL PROTECTED]
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}