I may be confused about what’s being discussed here, but isn’t this proposed IsNot
operator the same thing as <>
? I’ve been writing
If obj <> nil Then
literally my entire Xojo/REALbasic life. Am I doing it wrong?
I may be confused about what’s being discussed here, but isn’t this proposed IsNot
operator the same thing as <>
? I’ve been writing
If obj <> nil Then
literally my entire Xojo/REALbasic life. Am I doing it wrong?
IS is probably the tiniest bit safer than =
For instance if you have a class that implements operator_compare you can get fooled by = vs IS
Class Class1
Public Function operator_compare(rhs as Variant) as integer
break
End Function
end class
then have code like
Dim c As Class1
If c = Nil Then
Break
End If
If c Is Nil Then
Break
End If
c = New class1
If c = Nil Then
Break
End If
If c Is Nil Then
Break
End If
watch how the second check for nil using = behaves. Probably NOT what you expected.
So in this case IS is “safer” and more correct
Comparisons using IS check if the references refer to the same thing (Nil is actually a singleton at runtime)
Comparisons using =, <>, and other comparison operators will invoke operator_compare which can give different behaviour (ie comparing contents instead of references)
Ah… interesting. Thanks for that insight.
I should add that—for whatever reason—I always use Is rather than = when I’m comparing equality. I.e., a quick search through one of my larger projects found hundreds of
If obj Is Nil
and zero cases of
If obj = Nil
Yet for comparing inequality I’ve always done If obj <> nil
.