Forums

Comparison operators

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Comparison operators

Postby Charles » Sat Jul 02, 2011 2:13 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Comparison operators

Postby Charles » Tue Jul 12, 2011 9:24 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 34 guests