"not in" and "not is"

I can’t help but wonder.

if (a !in b)

reads “if a is not in b”

if (a !is B)

reads “if a not is B”
which is a bit awkward, “if a is not B” would be more natural which basically translates to

if (a is! B)

Was this a conscious choice? Has this some more nuanced rationale?

Parsing-wise !is and is! are equally easy. They are are just some kind of special token similar to other keywords. The syntax itself does not get ambigous.

It is a kind of inconsistency because it’s in and !in but is and is! but to me the natural language flow would more than offset the inconsistency.

1 Like

I’m thinking the opposite:

if(!isPrinted()) ...
if(a !in b) ...
if(! ... )...

the logical negation operator is always put in front of the things that matter.
Is there a reason to put it after the is?
That would be a bit odd…
I would ask the question: What does putting the ‘!’ after something mean?

The ! is boolean “not” and reads (at least in my head) as “not”.

if (!isPrinted()) ...

is already kinda weird.
“if not is printed then …” could be ideally reworded to “if [this] is not printed then …”

true, but trying to change almost every programming-language is a bit difficult…
Therefor if you can’t beat them. join them…
Therefor just comply and put the “not” before instead of after…

Because not is an exiting unary operator that negates the expression that follows it.

Realize that these are different expressions and are valid:

a !in b
a in! b

With operator overloading those become which are very different:

!b.contains(a)
b.not().contains(a)

But for ‘is’ you are asking that !is and is! be the same which would be weird for no real gain. Programmers are used to the idea that ! is a unary operator and it precedes the thing it inverts similar to the not operator in mathematics. It is only a little different here because it is inverting a verb and the English and Mathematics do things opposite. Better to stick with the formal mathematics definitions.

4 Likes

It’s not weird. Surely not as much as when it followed natural language logic exactly. Would you prefer

if (is!Printed())

to

if (!isPrinted())

as it’s more “natural”? I hope not.

Similarity to natural languages is good (mostly for beginners), but strict and simple rules are much more important (for both beginners and experts). Natural languages are terribly complicated and illogical, when it comes to more complicated stuff.

2 Likes

Since you said the word weird I assume you were directing that at me, but you seemed to think I was arguing the opposite of what I said. Your comments agreed with what I was saying.

@dalewking Indeed, we agree. My “weird” comes from an old comment (Oct '17) by @Philip-Trettner.