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

%hash«$key»:exists is true allways #6089

Closed
p6rt opened this issue Feb 21, 2017 · 4 comments
Closed

%hash«$key»:exists is true allways #6089

p6rt opened this issue Feb 21, 2017 · 4 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Feb 21, 2017

Migrated from rt.perl.org#130827 (status was 'rejected')

Searchable as RT130827$

@p6rt
Copy link
Author

p6rt commented Feb 21, 2017

From jarkko@bansu.fi

EXAMPLE​:
my %hash = foo => 1;
if %hash<a>​:exists {
say "not gonna print this";
}
my $key = 'b';
if %hash«$key»​:exists {
say "why i'm here";
}

OUTPUT​:
why i'm here

EXPECTED RESULT​:
(Should not print anything)

FROM #perl6 CHANNEL​:
14​:09 < masak> m​: my %hash = foo => 1; my $key = 'b'; if
%hash«$key»​:exists { say "why i'm here" }; say "alive"
14​:09 <+camelia> rakudo-moar 80e0bc​: OUTPUT​: «why i'm here␤alive␤»
14​:09 < masak> huh.
14​:10 < masak> I... I call bug.
14​:10 < masak> m​: my %hash = foo => 1; my $key = 'b'; if
%hash«$key»​:exists { say "why i'm here" }; say %hash.perl
14​:10 <+camelia> rakudo-moar 80e0bc​: OUTPUT​: «why i'm here␤{​:foo(1)}␤»

VERSION INFORMATION
$ perl6 -v
This is Rakudo version 2017.01 built on MoarVM version 2017.01
implementing Perl 6.c.

$ uname -a
Linux xprs 4.9.9-1-macbook #​1 SMP PREEMPT Mon Feb 13 17​:07​:28 EET 2017 x86_64 GNU/Linux

Best regards,

--
Jarkko

@p6rt
Copy link
Author

p6rt commented Feb 22, 2017

From @zoffixznet

On Tue, 21 Feb 2017 05​:15​:56 -0800, jarkko@​bansu.fi wrote​:

EXAMPLE​:
my %hash = foo => 1;
if %hash<a>​:exists {
say "not gonna print this";
}
my $key = 'b';
if %hash«$key»​:exists {
say "why i'm here";
}

OUTPUT​:
why i'm here

EXPECTED RESULT​:
(Should not print anything)

FROM #perl6 CHANNEL​:
14​:09 < masak> m​: my %hash = foo => 1; my $key = 'b'; if
%hash«$key»​:exists { say "why i'm here" }; say "alive"
14​:09 <+camelia> rakudo-moar 80e0bc​: OUTPUT​: «why i'm here␤alive␤»
14​:09 < masak> huh.
14​:10 < masak> I... I call bug.
14​:10 < masak> m​: my %hash = foo => 1; my $key = 'b'; if
%hash«$key»​:exists { say "why i'm here" }; say %hash.perl
14​:10 <+camelia> rakudo-moar 80e0bc​: OUTPUT​: «why i'm here␤{​:foo(1)}␤»

VERSION INFORMATION
$ perl6 -v
This is Rakudo version 2017.01 built on MoarVM version 2017.01
implementing Perl 6.c.

$ uname -a
Linux xprs 4.9.9-1-macbook #​1 SMP PREEMPT Mon Feb 13 17​:07​:28 EET 2017
x86_64 GNU/Linux

Best regards,

--
Jarkko

Thank you for the report. There was a discussion[^1] on the topic later on that clarified what
is happening in this particular case, and the behaviour is not a bug.

The interpolation syntax you're using doesn't just interpolate the variable's
contents, but also splits them up on whitespace (makes sense; it's as if the variable's content was
written in «...» directly)​:

  <Zoffix> m​: my $x = 'a b'; dd «$x»
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «slip("a", "b")␤»

The result is a slip, so that at the end you end up with a nice flat list​:

  <Zoffix> m​: my $x = 'a b'; dd «$x $x»
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «("a", "b", "a", "b")␤»

You always get a slip from just «$x», regardless of whether it ends up
as just one or multiple elements​:

  <Zoffix> m​: my $x = 'a'; dd «$x»
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «slip("a",)␤»
  <Zoffix> m​: my $x = 'a b'; dd «$x»
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «slip("a", "b")␤»

And if you do meant for the variable to be interpolated as just
one item, then use quotation marks; and in that case it does end up as on Str​:

  <Zoffix> m​: my $x = 'a b'; dd «"$x"»
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «"a b"␤»

So now, if we circle back to the original code​:

  %hash«$key»​:exists { say "why i'm here" };

It will always succeed, as long as $key is not an empty string, because you're always
giving a slice to lookup and getting a list back. And were different semantics used, you'd
end up with a case where %hash«$key»​:exists would tell you whether or not a key existed,
**but only if that key did not have any whitespace in it**; if it does, then the answer would
be always true.

The correct syntax you're looking for is​:

  %hash{$key}​:exists { say "why i'm here" };

That way you're passing whatever is in $key literally; so this is the way to refer to object
hashes as well​:

  <Zoffix> m​: my %h := :{ 42 => 'foo' }; say %h{42} # make a Hash with Int keys, give Int when looking up key
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «foo␤»
  <Zoffix> m​: my %h := :{ 42 => 'foo' }; say %h<42> # lookup fails, because we're giving a Str, not Int, key
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «(Any)␤»
  <Zoffix> m​: my %h := :{ 42 => 'foo' }; say %h«42» # same difference
  <camelia> rakudo-moar 80e0bc​: OUTPUT​: «(Any)␤»

Cheers,
ZZ

[1] https://irclog.perlgeek.de/perl6/2017-02-21#i_14137505

@p6rt
Copy link
Author

p6rt commented Feb 22, 2017

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

@p6rt
Copy link
Author

p6rt commented Feb 22, 2017

@zoffixznet - Status changed from 'open' to 'rejected'

@p6rt p6rt closed this as completed Feb 22, 2017
@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant