non-Boolean return type of "and" block

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?

No they don’t. In JS:

4 && 7

returns 7,

in python

4 and 7

Also returns 7.

AND never is required to return a boolean.

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.

¹ NB this is just an assumption.

Huh. I don’t know why we report the left value rather than the right value like everyone else if they’re both considered true.

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! :frowning: :frowning: :frowning:

offtopic Why is a comma a link? dr

Why I just used "offtopic" and "dr"

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

Why is a comma a link?

To get to the other side.

Aww

Funny Off-topic

That quote’s content is equivalent to this:

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).

(item # of [] in [ V])

What does it report if the first input doesn’t appear in the second (the list)? If it’s something falsy, like say 0, that would be a reason.

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).

useand script pic

… or I guess the other way around if it reports the first input. :~/

Yeah, it reports 0, but there’s already <[ V] contains [] ?> which serves the same purpose you’re describing here.

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.

But to do more than one thing with that value, you have to store it in a variable, and variables can’t be dropped on Boolean slots.

Yeah. We’d need something like the Scheme notation

(cond
  ((item # of (...) in (...)) -> (lambda (x) (replace item x of (...) with (...))))
  (else (...)))

where the “->” means “if this test turns out true, call this function with the value of the test as its input.” Maybe we should add that… :~)

That would work great for FIND FIRST (where the fallback is a falsey (empty string)).

Something i’ve seen AND used for are things like this:

someCondition && runFunction()

they can be used as compact if statements.