[CLOSED] Class operator IS lazarus / 61-Beginners ---------------------------------------------------------------------- julkas · 2020-05-06 16:47 When operator is raises exception ? Thanks. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyObj = class...var   x: TObject; ...  if x is TMyObj then... ---------------------------------------------------------------------- Thaddy · 2020-05-06 17:26 Too many mistakes... This works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program myobject;{$mode delphi}{$H+}type  TMyObj = class  end; var  x: TObject;begin  if x is TMyObj then (*  do something *);end. ---------------------------------------------------------------------- julkas · 2020-05-06 17:35 Quote from: Thaddy on May 06, 2020, 07:26:33 pmToo many mistakes... This works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program myobject;{$mode delphi}{$H+}type  TMyObj = class  end; var  x: TObject;begin  if x is TMyObj then (*  do something *);end.And another mistake Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program myobject;{$mode delphi}type  TMyObj = class  end; var  x: TObject;begin  x := TObject(1);  if x is TMyObj then    WriteLn('Mistake');end. ---------------------------------------------------------------------- lucamar · 2020-05-06 17:55 On to the real question: Quote from: julkas on May 06, 2020, 06:47:41 pmWhen operator is raises exception ? Thanks. The answer is never (or rather it never should). Note that it's equvalent to SomeObject.InheritsFrom(SomeClass) which, as a class method, shouldn't fail even if SomeObject is Nil ---------------------------------------------------------------------- julkas · 2020-05-06 18:45 Quote from: lucamar on May 06, 2020, 07:55:39 pmOn to the real question: Quote from: julkas on May 06, 2020, 06:47:41 pmWhen operator is raises exception ? Thanks. The answer is never (or rather it never should). Note that it's equvalent to SomeObject.InheritsFrom(SomeClass) which, as a class method, shouldn't fail even if SomeObject is Nil My last example compiles without error. Running fails. ---------------------------------------------------------------------- marcov · 2020-05-06 18:48 Quote from: julkas on May 06, 2020, 08:45:55 pmMy last example compiles without error. Running fails. Because it violates another rule: uninitialized. ---------------------------------------------------------------------- julkas · 2020-05-06 18:58 Quote from: marcov on May 06, 2020, 08:48:49 pmQuote from: julkas on May 06, 2020, 08:45:55 pmMy last example compiles without error. Running fails. Because it violates another rule: uninitialized. It's ok - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program myobject;{$mode delphi}type  TMyObj = class  end; var  x: TObject;begin  if x is TMyObj then    WriteLn('Mistake');end. ---------------------------------------------------------------------- lucamar · 2020-05-06 19:45 Quote from: marcov on May 06, 2020, 08:48:49 pmQuote from: julkas on May 06, 2020, 08:45:55 pmMy last example compiles without error. Running fails. Because it violates another rule: uninitialized. It shouldn't matter whether it's initialized or not, and in fact this works all right: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program myobject;{$mode delphi}type  TMyObj = class  end; var  x: TObject;begin  if x is TMyObj then    WriteLn('Mistake')  else begin  WriteLn('It isn''t!');    if not Assigned(x) then      WriteLn('In fact, it is nothing at all ... ;)');  end;end. As expected, it writes: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i It isn't!In fact, it is nothing at all ... ;) ---------------------------------------------------------------------- marcov · 2020-05-06 19:57 Quote from: lucamar on May 06, 2020, 09:45:47 pmQuote from: marcov on May 06, 2020, 08:48:49 pmQuote from: julkas on May 06, 2020, 08:45:55 pmMy last example compiles without error. Running fails. Because it violates another rule: uninitialized. It shouldn't matter whether it's initialized or not, and in fact this works all right: It should. In general the datasegment is zeroed out on startup however, but NIL is not a TMYObj, so that is ok. ---------------------------------------------------------------------- Remy Lebeau · 2020-05-06 21:50 Quote from: julkas on May 06, 2020, 06:47:41 pmWhen operator is raises exception ? When x does not point at a valid object in memory.  But even then, an exception is not guaranteed.  The is operator behaves as expected (ie, no exception) only when x is nil or points at a valid object.  Any other usage is undefined behavior. ---------------------------------------------------------------------- jamie · 2020-05-07 02:03 can't you return the class name without an instance? X.ClassName should work? So also the "IS" should work I would think, maybe not.. ---------------------------------------------------------------------- julkas · 2020-05-07 06:02 Quote from: Remy Lebeau on May 06, 2020, 11:50:28 pmQuote from: julkas on May 06, 2020, 06:47:41 pmWhen operator is raises exception ? When x does not point at a valid object in memory.  But even then, an exception is not guaranteed.  The is operator behaves as expected (ie, no exception) only when x is nil or points at a valid object.  Any other usage is undefined behavior. Clear answer. Thanks Remy. Related - https://stackoverflow.com/questions/44543343/when-is- correct-to-use-the-is-operator-in-delphi