Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalar.WHICH doesn't discriminate enough #5375

Open
p6rt opened this issue Jun 14, 2016 · 5 comments
Open

Scalar.WHICH doesn't discriminate enough #5375

p6rt opened this issue Jun 14, 2016 · 5 comments
Labels
RFC Request For Comments

Comments

@p6rt
Copy link

p6rt commented Jun 14, 2016

Migrated from rt.perl.org#128399 (status was 'open')

Searchable as RT128399$

@p6rt
Copy link
Author

p6rt commented Jun 14, 2016

From zefram@fysh.org

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.

-zefram

@p6rt
Copy link
Author

p6rt commented Jun 14, 2016

From @lizmat

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-archive.perl.org/perl6/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

@p6rt
Copy link
Author

p6rt commented Jun 14, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Jun 14, 2016

From zefram@fysh.org

Elizabeth Mattijsen via RT wrote​:

.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.

@​a[0].VAR.say
22
@​a[1].VAR.say
33
my $x = @​a[0].VAR
22
my $y = @​a[1].VAR
33
$x.WHAT.say
(Scalar)
$y.WHAT.say
(Scalar)
$x.say
22
$y.say
33
$x === $y
True

-zefram

@p6rt
Copy link
Author

p6rt commented Jun 15, 2016

From zefram@fysh.org

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.)

sub scalar-make(Mu $iv) { my $v = $iv; $v.VAR }
sub scalar-make (Mu $iv) { #`(Sub|92435288) ... }
sub scalar-get(Scalar​:D $s) { my $v := $s; $v }
sub scalar-get (Scalar​:D $s) { #`(Sub|92435440) ... }
sub scalar-set(Scalar​:D $s, Mu $nv) { my $v := $s; $v = $nv }
sub scalar-set (Scalar​:D $s, Mu $nv) { #`(Sub|92435592) ... }
my $x = scalar-make(22)
22
my $y = scalar-make(33)
33
scalar-get($x)
22
scalar-get($y)
33
scalar-set($x, 44)
44
scalar-get($x)
44
scalar-get($y)
33
$x.WHICH
Scalar|128188776
$y.WHICH
Scalar|128188776
$x === $y
True
scalar-get($x).WHICH
Int|44
scalar-get($y).WHICH
Int|33
scalar-get($x) === scalar-get($y)
False

-zefram

@p6rt p6rt added the RFC Request For Comments label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments
Projects
None yet
Development

No branches or pull requests

1 participant