4 ms·
"is" is the reference equality operator, that can't be overloaded, in Python. It having a different meaning in C++ is unfortunate. "is" seems to really mean "is
by jraph 4y ago
"is" is the reference equality operator, that can't be overloaded, in Python. It having a different meaning in C++ is unfortunate. "is" seems to really mean "is a" in C++. instanceof / isinstance has a related meaning in Java or JavaScript / Python and is a way more clear keyword than "is", which is a very ambiguous word.
- nmeofthestate 4y agoUsing 'is' and 'as' in this way is in line with C#.
- pjmlp 4y agoAnd Delphi before it.
- klyrs 4y agoComing from python, the meaning of this "is" is effectively "is" or "issubclass" for types and "isinstance" for value,type pairs. It's really not that much of a stretch. I'd question of C++ really needs to care what Python devs are going to think, though.
- jraph 4y agoWould it care about devs using multiple languages, switching back and forth, or people coming to C++ from other languages though? Of course program language designers need to care about the ergonomics of their languages, and how it sits in the bigger ecosystem, it's their job. If they only target experts specialized in one language who don't practice other languages, that's just wrong if you ask me. I was just suggesting a potential source of confusion, saying it's unfortunate. Not that this is wrong or anything. Now, someone pointed out that C# does the same thing with "is" and "as" (which I didn't know, I don't know C#) so it seems they did take inspiration from the ecosystem and did not invent a potentially confusing language construct alone. That does not remove the confusion, but it's actually good they took the same syntax for a feature that already exists in the neighborhood.
- klyrs 4y ago> Would it care about devs using multiple languages, switching back and forth, or people coming to C++ from other languages though? I'm not sure where we'd draw the line, and "nobody can reuse a reserved word that any other language uses unless they're conceptually identical" seems completely misguided. If you're using multiple langues and switching back and forth, it's your responsibility to know those languages. I use 5 languages frequently, "switching" at least several times per day. I'm a grayhair, so maybe I'm taking my experience for granted, but this seems like an unreasonable burden to put on language designers.
- jraph 4y ago> and "nobody can reuse a reserved word that any other language uses unless they're conceptually identical" seems completely misguided I agree with this, and this would be far away from what I stated (that the "collision" is unfortunate). > If you're using multiple langues and switching back and forth, it's your responsibility to know those languages Absolutely, but we are human beings, we make mistakes, confusions are source of mistakes. This is perhaps unavoidable, and maybe using "is" what the best decision despite this, but still unfortunate. Maybe Python created this confusion in the first place, if Delphi had "is", with a meaning close to the C++ one.
- chc 4y agoIf your mental model of C++ is viewing everything as implicit references like Python, this operator is the least of your problems. If you are not viewing C++ that way, there is no way you could confuse the two operators, because the Python one would make little sense in C++.
- avgcorrection 4y ago“To be” is a ridiculously overloaded. Python would not the right to call dibs.
- jraph 4y agoJust to be clear, risking of stating the obvious, when I wrote "overloaded", I was talking about operator overloading. In both C++ and Python, == can be overloaded, so if you need to be sure you are not dealing with an overloaded equality, "is" is convenient in Python. I'm not sure what I would do in C++, and I could imagine a world where "is" in C++ was like "==", but guaranteed to not be overloaded. Of course, Python can't reserve "to be".
- avgcorrection 4y agoJust to be clear: “to be” refers to the English language (no “to be” operator or this and that in programming). “Overloaded” means that it can mean a whole bunch of different things. This should be obvious from context.
- jraph 4y ago> This should be obvious from context Yes, I understood your comment. It's just that since you used the same word as me but with a different meaning, I wanted to make sure there was no misunderstanding.