I expect the “and” block to always return a Boolean value. However, it seems as though it returns the first input value if the input is not a Boolean value. Is this intentional, or is it a bug?
Other programming languages simply convert non-empty, non-false inputs to “true” so that the “and” block always returns a Boolean value. What is the purpose of this specific behavior in Snap?
It’s not specific to snap, in fact this is directly from the python documentation and backed up by testing it myself.
The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
So 4 and 7 results in 7, not True. This seems to also be the case for javascript. Snap is still slightly different, as it returns 4 instead, but it’s still a similar idea. I don’t actually know if this is completely accurate, but I think C++ does kind of do what you’re saying, cause it would make more sense for that to convert the values to booleans (well, it’s really just 0 and 1), and then return that instead of the original value.
For Python, the and operator returns the first operand if it is falsy, otherwise it returns the second. A falsy value is for example an empty string, False, None or nought as an integer or float. A truthy value is for example a non-empty string, True or an integer or float other than nought. I assume it is different in Snap! since according to some quick tests I just ran, JavaScript behaves the same way so it must have been integrated in Snap! the other way round.¹ and since neither of the inputs actually evaluates to <O false>, it returns the falsy value that got put in if one of the inputs is falsy and the first value if both are truthy.
I hope this helped.
Thanks for your answers. I now understand how the “and” block’s response works.
It would be helpful if a corresponding comment were added to the right-click help card for the “and” block so that other C++ programmers aren’t caught off guard by this as well.
I agree! Maybe Jens Mönig can explain why that’s the case. Scratch gets around this by only accepting hexagonal blocks in hexagonal slots, which always return true or false, which means that the AND, OR, and NOT blocks must also return only those two strings (Yes, they are strings, since there are no first-class booleans in Scratch). I have never used C++ so hearing that AND only ever returns true or false rather surprised me. By the way, i learnt about truthy and falsy values on freeCodeCamp.org, which has already been such a great help to me but most tragically doesn’t include tutorials for Snap!
Im using my style of tone indicator now cause I’m a silly guy and silly guys need silly reading guides.
italic <sup>s are small disclaimers that are unshortened <sub>s are the actual tone indicators, but I know nothing about them so I keep inventing them!!!
Used in post:
dr - don’t remove - assumes the question is genuine and not a request to have something removed as popularly implied
Technically scratch does return booleans instead of strings, and handles them differently. For instance, sticking a boolean into an add block returns 0 or 1, like you’d expect, but sticking a true or false string (from a join block) always returns 0. They’re just not visually distinct like they are in snap.
Also fun fact, there’s actually 2 reporters that can be dropped into boolean slots, (item ( v) of [ V]) and (item # of [] in [ V]) . Why these two specific blocks, especially the latter? Beats me. Not even variables can be dropped into boolean slots, and those would make much more sense.
To be honest, as a mostly python and javascript dev, this also caught me off guard. I’m super used to the OR operator returning the values, but I had never realized AND does the same (though I can’t think of any use case for it, but I use the OR behavior all the time).
I’m envisioning that once you determine that the item is in the list, you go on to do something with its index, e.g., delete the item or change it to something else.