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

Boolean operators return inconsistent results with undefined inputs #246

Closed
deepilla opened this issue Jul 10, 2018 · 5 comments · Fixed by #269
Closed

Boolean operators return inconsistent results with undefined inputs #246

deepilla opened this issue Jul 10, 2018 · 5 comments · Fixed by #269
Labels

Comments

@deepilla
Copy link
Contributor

deepilla commented Jul 10, 2018

This issue was first raised in a Slack post. I'm reposting here (with some additional info) so it doesn't get lost.

The original poster was using the or operator with one falsy value and one undefined value (a non-existent path expression). They noticed that the or operator sometimes returned false and sometimes returned undefined depending on the order of the operands.

I tried various combinations of the or and and operators with one or more undefined values (named foo and bar) and got the following results.

Or

true or foo    // true
foo or true    // true

false or foo   // undefined
foo or false   // false

foo or bar     // undefined

And

true and foo    // undefined
foo and true    // undefined

false and foo   // false
foo and false   // undefined

foo and bar     // undefined

Note that:

  1. Combining a falsy value with an undefined value does indeed give different results depending on the order of the operands.

  2. There's a mix of boolean and undefined results, e.g. true or foo returns true but true and foo returns undefined.

These are the only binary operations that produce such inconsistent results. Numeric operators (like + and -) return undefined if either operand is undefined. Comparison operators (like = and <) return false if either operand is undefined. And the concatenation operator (&) treats undefined operands as empty strings.

Boolean operators should have a similarly well-defined approach to undefined inputs. I suggest one of the following:

  1. Treat undefined operands as false (this would turn all of the undefined results above to false).

  2. Return undefined if either operand is undefined (all of the above expressions would then evaluate to undefined).

Any thoughts on this, @andrew-coleman?

@wnm3
Copy link

wnm3 commented Jul 10, 2018

If we are considering increasing the use of undefined, I think we need a function to test for this state (unless = "undefined" will work). Perhaps an $isDefined() or $exists() would be helpful here.

@jhorbulyk
Copy link
Contributor

Perhaps an $isDefined() or $exists() would be helpful here.

@wnm3 Like this $exists() function?

@wnm3
Copy link

wnm3 commented Jul 10, 2018

Precisely like that ;^) thank you -- I hadn't found this when I looked.

@andrew-coleman
Copy link
Member

Thanks for raising this @deepilla. I think I favour your first suggestion

  1. Treat undefined operands as false (this would turn all of the undefined results above to false).

This would be consistent with the comparison operators

@wnm3
Copy link

wnm3 commented Jul 18, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants