{"id":652,"date":"2020-03-18T11:39:00","date_gmt":"2020-03-18T17:39:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=652"},"modified":"2020-03-10T12:18:51","modified_gmt":"2020-03-10T18:18:51","slug":"is-nil-or-nil","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/03\/18\/is-nil-or-nil\/","title":{"rendered":"Is nil or = nil ?"},"content":{"rendered":"\n<p>Often in code you might see checks for NIL written as <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if foo = nil then<\/code><\/pre>\n\n\n\n<p>and sometimes you might see<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if foo is nil<\/code><\/pre>\n\n\n\n<p>Whats the difference between the two ?<\/p>\n\n\n\n<p>In Xojo the IS operator compares the references. It doesn&#8217;t compare the contents of the objects in any way. It answers the question &#8220;do these two references refer to the exact same instance&#8221; &#8211; in some languages this would be comparing whether two pointers point to the same location.<\/p>\n\n\n\n<p>= will do much the same IF you have NOT implemented operator_compare for your specific classes. However if you HAVE implemented operator_compare then you can get VERY different results as your implementation of operator_compare will be called, even if one of the items is nil. In the following code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if foo = nil then<\/code><\/pre>\n\n\n\n<p>operator_compare for the instance foo would be called with the rhs parameter as NIL.<\/p>\n\n\n\n<p>You can try this for yourself very quickly.<\/p>\n\n\n\n<p>Create a new desktop project. In that new project add a Class named <em>myClass<\/em>. Add a method to <em>myClass<\/em>,<em> Public operator_compare(rhs as myClass) as Integer<\/em><\/p>\n\n\n\n<p>Simply put a break statement in the code<\/p>\n\n\n\n<p>Now in Window1.open put<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim foo as new myClass\n\nif foo = nil then \/\/ you will hit your break statement in the class\nend if\n\nif foo is nil then \/\/ you will NOT hit the break statement in the class\nend if\n<\/code><\/pre>\n\n\n\n<p>The advice here is to be REALLY careful about when you use IS or =, or other comparison operators like >= &lt;= &lt;> etc, as they could mean VERY different things. And without reviewing code you can&#8217;t be sure whether a class does or does not have operator_compare implemented.<\/p>\n\n\n\n<p>Use IS when you mean &#8220;are these the referring to the same instance&#8221;<\/p>\n\n\n\n<p>Use the others when you _may_ mean something else.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often in code you might see checks for NIL written as and sometimes you might see Whats the difference between the two ? In Xojo the IS operator compares the references. It doesn&#8217;t compare the contents of the objects in any way. It answers the question &#8220;do these two references refer to the exact same &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/03\/18\/is-nil-or-nil\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Is nil or = nil ?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-652","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=652"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/652\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/652\/revisions\/657"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}