Comparison operators
Posted: Sat Jul 02, 2011 2:13 pm
Someone recently wrote:
But this is not valid as it's checking to see if .foo is the identical object to a string literal. Even if .foo was initialized with '', there is no guarantee that the comparison will be valid. And if it comes from something else like "someStringBuilder.toString" then it probably won't be.
So as a reminder:
I suppose Cobra could issue a warning or error for using "is" or "is not" where either side is a literal string, list, dictionary or set. Anyone anticipate any problems with that?
It could probably also be done with "someVar is [not] Foo()" since that will always be false.
Comments are welcome.
if .foo is not '', ...
But this is not valid as it's checking to see if .foo is the identical object to a string literal. Even if .foo was initialized with '', there is no guarantee that the comparison will be valid. And if it comes from something else like "someStringBuilder.toString" then it probably won't be.
So as a reminder:
# Use "is" and "is not" to check if objects a and b are the exact same object (or not).
# You can think of this as a "pointer comparison" or "reference comparison" if you like.
if a is b, ...
if a is not b, ...
# Use "==" and "<>" to check if objects a and b have equal values (or not).
# You can think of this as "value comparison" or "contents comparison".
if a == b, ...
if a <> b, ...
I suppose Cobra could issue a warning or error for using "is" or "is not" where either side is a literal string, list, dictionary or set. Anyone anticipate any problems with that?
It could probably also be done with "someVar is [not] Foo()" since that will always be false.
Comments are welcome.