Skip Menu |
Report information
Id: 128399
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: zefram [at] fysh.org
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



To: rakudobug [...] perl.org
Subject: Scalar.WHICH doesn't discriminate enough
From: Zefram <zefram [...] fysh.org>
Date: Tue, 14 Jun 2016 05:27:21 +0100
Download (untitled) / with headers
text/plain 616b
Show quoted text
> my @a = 22,33
[22 33] Show quoted text
> @a[0]
22 Show quoted text
> @a[1]
33 Show quoted text
> @a[0].VAR.WHICH
Scalar|100414504 Show quoted text
> @a[1].VAR.WHICH
Scalar|100414504 Show quoted text
> @a[0].VAR === @a[1].VAR
True Behaviourally the two scalar objects are clearly distinct, as they should be. .WHICH errs in advertising the same identity for both, and === follows suit in saying that they are identical. All scalar containers created for a single array get this treatment, even ones created later by pushing more elements onto the array. Scalars created separately, for other arrays or for standalone scalar variables, correctly get different identities shown by .WHICH. -zefram
To: "Zefram (via RT)" <perl6-bugs-followup [...] perl.org>
From: Elizabeth Mattijsen <liz [...] dijkmat.nl>
Subject: Re: [perl #128399] Scalar.WHICH doesn't discriminate enough
Date: Tue, 14 Jun 2016 09:33:12 +0200
Download (untitled) / with headers
text/plain 1.1k
Show quoted text
> On 14 Jun 2016, at 06:27, Zefram (via RT) <perl6-bugs-followup@perl.org> wrote: > > # New Ticket Created by Zefram > # Please include the string: [perl #128399] > # in the subject line of all future correspondence about this issue. > # <URL: https://rt.perl.org/Ticket/Display.html?id=128399 > > >
>> my @a = 22,33
> [22 33]
>> @a[0]
> 22
>> @a[1]
> 33
>> @a[0].VAR.WHICH
> Scalar|100414504
>> @a[1].VAR.WHICH
> Scalar|100414504
>> @a[0].VAR === @a[1].VAR
> True > > Behaviourally the two scalar objects are clearly distinct, as they > should be. .WHICH errs in advertising the same identity for both, and === > follows suit in saying that they are identical. All scalar containers > created for a single array get this treatment, even ones created later > by pushing more elements onto the array. Scalars created separately, > for other arrays or for standalone scalar variables, correctly get > different identities shown by .WHICH.
ENOTABUG .VAR reveals the properties of the underlying container of the object it is used on. For an array, this *IS* the same for each element. The same applies to hashes: $ 6 'my %h = a => 42, b => 666; say %h<a>.VAR === %h<b>.VAR’ True Liz
To: Elizabeth Mattijsen via RT <perl6-bugs-followup [...] perl.org>
From: Zefram <zefram [...] fysh.org>
Subject: Re: [perl #128399] Scalar.WHICH doesn't discriminate enough
Date: Tue, 14 Jun 2016 09:15:12 +0100
Download (untitled) / with headers
text/plain 788b
Elizabeth Mattijsen via RT wrote: Show quoted text
>.VAR reveals the properties of the underlying container of the object >it is used on. For an array, this *IS* the same for each element.
No, .VAR really is yielding two different objects that behave differently. It's not just the ancillary properties of the container; the contained value can be reached from the .VAR object. I don't see anything as simple to use as getter and setter methods on Scalar to access the contained value, but method calls on the Scalar that are not applicable to the Scalar itself, such as .say, get passed through to the contained value. Show quoted text
> @a[0].VAR.say
22 Show quoted text
> @a[1].VAR.say
33 Show quoted text
> my $x = @a[0].VAR
22 Show quoted text
> my $y = @a[1].VAR
33 Show quoted text
> $x.WHAT.say
(Scalar) Show quoted text
> $y.WHAT.say
(Scalar) Show quoted text
> $x.say
22 Show quoted text
> $y.say
33 Show quoted text
> $x === $y
True -zefram
Subject: Re: [perl #128399] Scalar.WHICH doesn't discriminate enough
From: Zefram <zefram [...] fysh.org>
To: Elizabeth Mattijsen via RT <perl6-bugs-followup [...] perl.org>
Date: Wed, 15 Jun 2016 05:00:23 +0100
Download (untitled) / with headers
text/plain 1.1k
Additional information: one also gets identical .WHICH values for scalars that are not actually part of the same array but arise from a single declaration that executes multiple times. This can happen for a declaration of an array variable or of a scalar variable. Here's another formulation of the demo, which illustrates the above and also makes clearer the access of contained value via the container object. (I wish the Scalar class were natively as easy to use explicitly as the functions here make it.) Show quoted text
> sub scalar-make(Mu $iv) { my $v = $iv; $v.VAR }
sub scalar-make (Mu $iv) { #`(Sub|92435288) ... } Show quoted text
> sub scalar-get(Scalar:D $s) { my $v := $s; $v }
sub scalar-get (Scalar:D $s) { #`(Sub|92435440) ... } Show quoted text
> sub scalar-set(Scalar:D $s, Mu $nv) { my $v := $s; $v = $nv }
sub scalar-set (Scalar:D $s, Mu $nv) { #`(Sub|92435592) ... } Show quoted text
> my $x = scalar-make(22)
22 Show quoted text
> my $y = scalar-make(33)
33 Show quoted text
> scalar-get($x)
22 Show quoted text
> scalar-get($y)
33 Show quoted text
> scalar-set($x, 44)
44 Show quoted text
> scalar-get($x)
44 Show quoted text
> scalar-get($y)
33 Show quoted text
> $x.WHICH
Scalar|128188776 Show quoted text
> $y.WHICH
Scalar|128188776 Show quoted text
> $x === $y
True Show quoted text
> scalar-get($x).WHICH
Int|44 Show quoted text
> scalar-get($y).WHICH
Int|33 Show quoted text
> scalar-get($x) === scalar-get($y)
False -zefram


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org