gleam-lang / website

🏡 Gleam's website and guide
https://gleam.run
Apache License 2.0
87 stars 171 forks source link

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

Open hikari-no-yume opened 4 months ago

hikari-no-yume commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

Agreed. Try and keep it simple.