Why can there only be one active helper lazarus / 62-General ---------------------------------------------------------------------- Warfley · 2021-02-14 01:31 Hey, I was wondering why there can only be a single active helper for a given type. To use more then one inheritance is required, which means the developer of each helper needs to know every other helper (or at least the latest helper). This is a severe limitation, especially when considering what helper types try to accomplish. Because helper types allow adding functionality to an existing type without having to touch the implementation of that type. But if you have multiple helper types  for the same type you want to use simultaneously, you need to edit at least one of them to bring them into a inheritance hierarchy (if they already have inheritances, you might need to edit all of them). So in effect, the feature that is made to avoid having to edit other types, requires you to edit other types. Tbh. this sounds like a bad design decision And not to forget, if you have type helpers in third party libraries, you get another problem, because the license of the library might not allow changes to them at all, meaning this could even be a dead end. So in conclusion, type helpers require to know all other typehlpers to be active at time of creating that type helper in order to create the inheritance chain. This means they are completely unfit for being provided by libraries or by larger projects that are developed in modular fashion. This might now sound a little bit abstract, so let me give you a practical example on this. So I am writing a lot of custom types lately, all of them pretty general and I wanted to package them up in independent units, so I don't need to include a large package every time I for example only need a single unit of them. Most of these types are just representing some data, so I wanted to make them streamable by extending TStream with a class helper, so after including the unit, you can use stream.WriteXXX or stream.ReadXXX so it works with all streams, regardless of network streams or Filestreams or whatever. This is now completely off the table, because I have to now combine multiple totally unrelated units, which is exactly the thing I don't want to. But honestly, this usecase is pretty much exactly what I thought helper types are made for. I mean they are not really a technical solution to any problem, they are just syntactic sugar to clean up the code a little. But with that limitation they completely fail at that. So I am left wondering simply why... Are typehelpers simply not intendet to be used in larger projects or libraries? From my point of view, helper types seem completely useless thanks to that limitation ---------------------------------------------------------------------- Kays · 2021-02-14 01:42 Quote from: Warfley on February 14, 2021, 02:31:04 am[…] why […]Without looking into the details I say: implementation details. FYI, there will be {$modeSwitch multiHelpers}. ---------------------------------------------------------------------- PascalDragon · 2021-02-14 11:40 Quote from: Warfley on February 14, 2021, 02:31:04 amSo I am left wondering simply why... Are typehelpers simply not intendet to be used in larger projects or libraries? From my point of view, helper types seem completely useless thanks to that limitation The reason for this is Delphi compatibility. And yes, originally (in Delphi) helper types where only intended for workarounds. For example the current Delphi documentation still states this: QuoteClass and record helpers provide a way to extend a type, but they should not be viewed as a design tool to be used when developing new code. For new code you should always rely on normal class inheritance and interface implementations. We don't agree with this view, thus - as Kays mentioned - trunk was extended some time ago with the MultiHelpers modeswitch which allows for multiple helpers to be in scope for a single type. As this has an influence on how helpers behave (especially if there is mixture of methods from different helpers) this modeswitch is not enabled by default. ---------------------------------------------------------------------- Warfley · 2021-02-14 15:16 Ah delphi compatibility. This somehow manages to be the greatest strength and the greatest weakness of the fpc and Lazarus. I sometimes wonder how the language could look like if it wasn't for Delphi compatibility. That said, I probably wouldn't know of fpc and lazarus without it ---------------------------------------------------------------------- lucamar · 2021-02-14 17:19 Quote from: jamie on February 14, 2021, 03:12:50 pmah, where is this mode switch and does it work like this ? [etc] It works in trunk only, IIRC (I don't have a trunk install here), and it works transparently: you call SomeVar.AHelperMethod(); or TSomeType.AHelperMethod(); and the compiler traverses the chain of helpers until it finds a match; basically as if it were a class but without needing inheritance. ---------------------------------------------------------------------- MarkMLl · 2021-02-14 18:08 My understanding is that in Object Pascal, a pointer to an object has to be fully declared before use. This compares with Modula-2 where pointers can be opaque. Quote from: PascalDragon on February 14, 2021, 12:40:17 pmQuoteClass and record helpers provide a way to extend a type, but they should not be viewed as a design tool to be used when developing new code. For new code you should always rely on normal class inheritance and interface implementations. We don't agree with this view, thus - as Kays mentioned - trunk was extended some time ago with the MultiHelpers modeswitch which allows for multiple helpers to be in scope for a single type. As this has an influence on how helpers behave (especially if there is mixture of methods from different helpers) this modeswitch is not enabled by default. Hence I think that Delphi has overlooked a valid use case: 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   TCMS50DPlusForm = class(TForm){ ... }  private    fcommsThread: TThread; { ... } implementation type  TcommsThread= class(TThread)  strict private    serialHandle: TSerialHandle;    operation: TOperation;    messageStr: string;    procedure msgShim;  protected  procedure Execute; override;  public    constructor Create(CreateSuspended: Boolean; mySerialHandle: TSerialHandle;                                                              op: TOperation);    procedure writerPlaybackShim;    procedure writerLiveShim;  end;   TCMS50DPlusFormH=                     (* Avoid circular definition            *)    class helper for TCMS50DPlusForm    protected      function CommsThread(): TCommsThread; inline;    end;  (* This helper avoids problems with circular references, in particular many  occurrences of things like TCommsThread(CMS50DPlusForm.CommsThread).Synchronize()  in favour of the somewhat more concise CMS50DPlusForm.CommsThread.Synchronize().*)function TCMS50DPlusFormH.CommsThread(): TCommsThread; inline; begin  result := fCommsThread as TcommsThreadend { TCMS50DPlusFormH.CommsThread } ;  However that only requires one helper to be accessible at a time, which I don't think worked cleanly with the initial implementation: if my understanding is correct you still had to chain class helpers even if you were never interested in multiple helpers being i-scope. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2021-02-15 08:14 Quote from: jamie on February 14, 2021, 03:12:50 pmah, where is this mode switch and does it work like this ? See the announcement mail. Quote from: jamie on February 14, 2021, 03:12:50 pmI know you can inherit from an existing one and it works well, I use it greatly, something that does not seem to yet work in Delphi it seems, at least the last time I checked I couldn't add to , I had to create a new one. Delphi allows inheritance only for class helpers, FPC also allows it for record/type helpers.