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

Another possible trap: %h<a b>:exists #1242

Closed
AlexDaniel opened this issue Mar 9, 2017 · 3 comments
Closed

Another possible trap: %h<a b>:exists #1242

AlexDaniel opened this issue Mar 9, 2017 · 3 comments
Labels
docs Documentation issue (primary issue type) trap

Comments

@AlexDaniel
Copy link
Member

AlexDaniel commented Mar 9, 2017

Not sure if this is a big enough trap, but it just got me, so…

I had code like this:

my %h = <a 1 b 2>;
say hello if %h<a>:exists;
say world if %h<x>:exists;

Output:

hello

Then I decided to check for more than one element, and using slices is what came to my mind immediately:

my %h = <a 1 b 2>;
say hello if %h<a b>:exists;
say world if %h<x y>:exists;

Output:

hello
world

Not exactly what I hoped for!

Thinking about it, it makes sense. Still, perhaps it is worth to mention something like this as one of the traps (are there any other traps similar to this one?).

@AlexDaniel AlexDaniel added the docs Documentation issue (primary issue type) label Mar 9, 2017
@trosel
Copy link
Contributor

trosel commented Mar 9, 2017

@AlexDaniel Why does that make sense? What happened exactly?

@b2gills
Copy link
Contributor

b2gills commented Mar 9, 2017

%h<x y>:exists

returns a list of 2 elements, and that list is True

So you really need something like this

say ‘world’ if all %h<x y>:exists;

@AlexDaniel
Copy link
Member Author

Yes, depending on what you want exactly, you should use any or all.

The fact that it returns the element itself (and not a list with one element) is a possible source for other traps. It makes sense because this is what you mean in most cases, but when you start generalizing you stumble upon completely different behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) trap
Projects
None yet
Development

No branches or pull requests

3 participants