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

Confusing documentation of && and ||: what does it mean that they are “boolean” and not “logical”? #335

Open
hikari-no-yume opened this issue Mar 13, 2024 · 7 comments

Comments

@hikari-no-yume
Copy link

hikari-no-yume commented Mar 13, 2024

Hi, I've been reading some of the “cheatsheets” to try to understand this new language.

The operator comparison table for PHP is very confusing when it comes to the boolean operators:

Operator PHP Gleam Notes
Boolean and && && In Gleam both values must be Bool
Logical and && Not available in Gleam
Boolean or || || In Gleam both values must be Bool
Logical or || Not available in Gleam
Boolean not xor Not available in Gleam
Boolean not ! ! In Gleam both values must be Bool

There are two obvious mistakes (the first “Boolean not” is clearly meant to be “Boolean exclusive or”, and ! only takes one value), but it's not what this issue is about.

What I don't understand is how PHP's && and || can simultaneously be both available and not available in Gleam. I'm also confused about this “Boolean” versus ”Logical” distinction. These aren't bitwise operators, they operate on Booleans. Looking at the cheatsheet for Erlang, I guess it might be about short-circuiting? But && and || always short-circuit in PHP, so this doesn't make sense. Alternatively maybe the “Logical” rows are meant to have the PHP and and or operators, but that would also be confusing, because these operators behave identically to && and ||aside from precedence, so they arguably aren't missing an equivalent in Gleam. In general, I'm not sure what “Boolean” versus ”Logical” should imply.

So, this section probably needs to be re-written by someone who understands what this is meant to mean? If I can make a suggestion about the other cheatsheets, they might be clearer if they said “short-circuiting” and “eager” rather than “Boolean” and ”Logical”. It was only when I looked at the Erlang one that I was able to piece together what this might mean, since its operator names suggested a short-circuiting distinction.

@natsukagami
Copy link

Coming from Elixir, "logical AND and OR" means "AND and OR operators that treat everything except nil and false as truthy values". They are a cute way to express things like default values "opt = nullableOpt || defaultVal".
I suppose many dynamically typed languages have the same concept.
In Elixir however || and && are "logical", while and and or (as operators) will crash if they don't receive booleans, hence the distinction.

@inoas
Copy link
Contributor

inoas commented Mar 13, 2024

the xor is both boolean and logical in php afaiu
it does not exist in gleam
"Boolean not" is wrong there must be "boolean xor" and "logical xor"
same for the not operator:

php -r "var_dump(!'');
true

this has nothing to do with short ciruting which I think all of these here are.

in erlang and vs and/or vs andalso/orelse make a difference afair in terms of short circuit, where the latter is and the earlier is not.

@hikari-no-yume
Copy link
Author

Is this about whether it accepts non-boolean values or returns them? Because PHP's || and && always return a boolean, but accept non-boolean values; on the other hand, PHP's ?: returns the operand unchanged.

@inoas
Copy link
Contributor

inoas commented Mar 14, 2024

Is this about whether it accepts non-boolean values or returns them? Because PHP's || and && always return a boolean, but accept non-boolean values; on the other hand, PHP's ?: returns the operand unchanged.

i think so, yes.

@lpil
Copy link
Member

lpil commented Mar 15, 2024

Do we have a way to move forward here? I think we could remove the logical/boolean split perhaps and just say that in Gleam it always wants bools

@hikari-no-yume
Copy link
Author

I think that sounds good. I notice there's also this logical/boolean split for the Python page. I would suggest dropping the split there too.

@lpil
Copy link
Member

lpil commented Mar 15, 2024

Agreed. Try and keep it simple.

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

No branches or pull requests

4 participants