Apply case to object class lazarus / 62-General ---------------------------------------------------------------------- MarkMLl · 2022-03-30 07:58 Given e.g. a pointer in a TList, what is the correct case syntax to efficiently decide what (instance of a) class it represents? I think what I'm looking for is the equivalent of "is", but usable for selecting from a list of types. Assuming that this works, is it a classic scalar case, or is it implemented like case of strings? Are there any FPC version number quirks that I need to document? I can't find anything in the HTML manual, and the links e.g. ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf are still broken. MarkMLl ---------------------------------------------------------------------- howardpc · 2022-03-30 08:20 I think it has to be string-based. 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   if Assigned(FList[i]) and FList[i].InheritsFrom(TObject) then    className := TObject(FList[i]).ClassName;Then use className in a case or wherever. ---------------------------------------------------------------------- MarkMLl · 2022-03-30 08:25 Quote from: howardpc on March 30, 2022, 10:20:40 amI think it has to be string-based. 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 if Assigned(FList[i]) and (FList[i] is TObject) then    objName := TObject(FList[i]).ClassName;Then use objName in a case statement or wherever. TFT, ouch. So from a performance POV I'd be better sticking to if ... is. MarkMLl ---------------------------------------------------------------------- Warfley · 2022-03-30 08:27 The problem here is that the "is" operator does more than just equality, it checks inheritance. So consider the following: 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 var o: TObject;begin  o := TStringList.Create;  if o.ClassType = TStrings then    WriteLn('Is TStrings =');  if o is TStrings then    Writeln('Is TStrings is'); You can see that only the second one is true. So this is not a simple equality operation, and therefore I don't think a case would cut it Quote from: howardpc on March 30, 2022, 10:20:40 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if Assigned(FList[i]) and FList[i].InheritsFrom(TObject) then    className := TObject(FList[i]).ClassName;As all classes inherit from TObject, the InheritsFrom seems pointless tbh ---------------------------------------------------------------------- MarkMLl · 2022-03-30 08:36 Quote from: Warfley on March 30, 2022, 10:27:14 amThe problem here is that the "is" operator does more than just equality, it checks inheritance. ... You can see that only the second one is true. So this is not a simple equality operation, and therefore I don't think a case would cut it Yes, agreed. And I've already determined that e.g. 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 case TObject(self[i]).ClassType of  TStringList ;otherwiseend;  doesn't work ("expansions.pas(387,33) Error: Ordinal or string expression expected"). In the current case I'll only need a very small number of cases, typically a stringlist (being used for temporary storage of a parsed assembler-like line) and a binary representation, so it's no great issue. MarkMLl ---------------------------------------------------------------------- howardpc · 2022-03-30 08:48 Quote from: Warfley on March 30, 2022, 10:27:14 amAs all classes inherit from TObject, the InheritsFrom seems pointless tbh You're quite correct. I was thinking to protect against the TList pointer possibly not referencing an object. However, until we have smart pointers I don't think this is possible. ---------------------------------------------------------------------- MarkMLl · 2022-03-30 08:56 Assuming that there isn't something that we've overlooked, I think the following is probably acceptable practice which avoids having deeply nested conditionals which (IMO) obfuscate structure. 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   for i := 0 to Count - 1 do            (* Every object in expansions list      *)    if Assigned(self[i]) then begin       if TObject(self[i]) is TStringList then begin        ...        continue      end;       if TObject(self[i]) is TSomethingElse then begin        ...        continue      end    end;    raise Exception.Create('Internal error: attempting to dump unhandled.')end;  MarkMLl ---------------------------------------------------------------------- Warfley · 2022-03-30 09:38 Why not use else-if? While it is theoretically nested, both Lazarus auto indentation as well as the code formatter make this flat: 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 if obj is TClass1 thenbegin endelse if obj is TClass2 thenbegin endelse if ...I mean sure if you are going to look at the AST it is nested, but the code is flat, enforced both by the IDE as well as the auto code formatter, and as someone who regularly uses c like languages, let me tell you, forgetting a break in a switch case (what you are turning this effectively into) is a very easy mistake to do ---------------------------------------------------------------------- MarkMLl · 2022-03-30 09:59 Quote from: Warfley on March 30, 2022, 11:38:15 amI mean sure if you are going to look at the AST it is nested, but the code is flat, enforced both by the IDE as well as the auto code formatter, and as someone who regularly uses c like languages, let me tell you, forgetting a break in a switch case (what you are turning this effectively into) is a very easy mistake to do At the same time, my own feeling- although clearly there is personal preference and personal formatting convention (roughly translated: habit :-) involved- is that there is an equal or possibly greater risk of messing up the final "catch all" exception or assertion resulting in even more difficult bugs. I'm simply not used to that particular layout convention, and uncomfortable with it. OTOH, Modula-2 has an explicit "ELSIF" but in that case there is a mandatory END which eliminates many potential problems. In any event, a missing break/continue/exit will be caught by that exception. I'm obviously aware that continue and exit stand accused of being "closet GOTO statements", and have been party to that debate since the mid-80s. I'm obviously also aware of Dijkstra's oft-quoted reservations about the use of GOTO, but would point out that he later expressed concern that it was being taken as a far more stringent religious pronouncement than he'd intended. So I think that on the balance I'll use the layout I've proposed in a very limited number of situations (dumping a list and deleting elements from it), but with appropriate comment describing what the preferred structure (i.e. some form of case) would be. A simpler and less controversial alternative would be to have a simple "is" determining whether a list item is a standard stringlist, plus a case based on a field of a locally-defined class. But I still feel that there would be a real risk there of allocating some other class and forgetting to update one of the places that that simple test was being used. MarkMLl ---------------------------------------------------------------------- Warfley · 2022-03-30 11:19 Quote from: MarkMLl on March 30, 2022, 11:59:52 amA simpler and less controversial alternative would be to have a simple "is" determining whether a list item is a standard stringlist, plus a case based on a field of a locally-defined class. But I still feel that there would be a real risk there of allocating some other class and forgetting to update one of the places that that simple test was being used. For this case, you should simply as for casting. It has a built in check and throws an exception: 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 if obj is TClass1 thenbegin  ...endelse // implicit assumption that there are only TClass1 or TClass2 herebegin  cls2Obj := obj as TClass2; // if implicit assumption does not hold, this will cause an exceptionend; ---------------------------------------------------------------------- MarkMLl · 2022-03-30 12:01 Quote from: Warfley on March 30, 2022, 01:19:01 pmFor this case, you should simply as for casting. It has a built in check and throws an exception: I agree with the general point, although that requires that a pointer from a TList be first cast to a TObject before the as operator can be used. I should at this point probably be subclassing TList to something like TObjectList, but for something which will be referenced several thousand times per program run I don't fancy adding any more extra layers than necessary... I'm sure there's lots of fettling I can still do but I think I've just about got something usable for the moment. The application is disassembling a capture from an old logic analyzer, and I'm specifying bit patterns etc. using a notation intended to irritate high- and lo-level language types equally (e.g. ; serves both as a Pascal-style separator and as a comment marker... that took some fiddling to get right). Later: I've added an Objects property to supplement TList's, and it turns out that it's only used in a couple of places anyway and those infrequently. However it will probably be used a bit more once I start doing a JIT conversion of (the compiled) text descriptions of patterns and actions to something binary for efficient execution: the entire collection of patterns will be consulted once for each stored state. MarkMLl