Page 1 of 1

Comparison operators

PostPosted: Sat Jul 02, 2011 2:13 pm
by Charles
Someone recently wrote:
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.

Re: Comparison operators

PostPosted: Tue Jul 12, 2011 9:24 pm
by Charles
This is done now for "is" and "is not" which will generate a warning if either side is a string literal or other type of object literal (list, set, dictionary, array).

Neither the test suite nor the standard library suffered any new warnings, so this should be good. If you have any problems, let me know.

Down the road I may make it an error as I cannot think of any conditions where it would be required or beneficial.