David Schwartz 426 Posted April 30, 2019 I'm looking for any kind of discussion about the use of ctors and dtors within class helpers. I've seen stuff that says you cannot put class ctors and dtors in them, as well as examples that DO THAT. I don't want to do that; but I am trying to figure out how they get called if they're normal ctors and dtors. I've read that there are other limitations of things within helpers, but ... where do I find a complete discussion of class helpers? The only thing I've found on Delphi's wiki shows adding properties and methods, not ctors and dtors. SO hasn't turned up much of anything useful either. Share this post Link to post
Dave Nottage 557 Posted April 30, 2019 48 minutes ago, David Schwartz said: The only thing I've found on Delphi's wiki shows adding properties and methods, not ctors and dtors. SO hasn't turned up much of anything useful either. Probably because that's the sum total of what you can do with class helpers. If you specify what you actually want to achieve, there might be a different solution. Share this post Link to post
Stefan Glienke 2002 Posted April 30, 2019 (edited) Took me 10 seconds to try it out. Ctor can be added, dtor not (E2295). Which makes sense if you think about it. Adding a different way to create new instances depending on if the helper is there or not is not a problem. But not how existing instances are being destroyed. Edited April 30, 2019 by Stefan Glienke Share this post Link to post
A.M. Hoornweg 144 Posted April 30, 2019 (edited) Class helpers are merely a bunch of procedures and functions grouped together that mimic methods of a class. Each method has an invisible first parameter that passes the object and treats it as if it were "self". So literally nothing gets constructed or destructed, they're merely procedures/functions that act on an object. The advantage of class helpers is that one can extend a class, without actually inheriting from it, and that everything is neatly wrapped inside the namespace of that class. As an analogy, it's a bit like having an optional side car attached to a motor cycle, instead of permanently changing the vehicle itself. The major disadvantage of class helpers is that there can only be one helper "in scope" for your code (so the sequence of units in your "uses" clause suddenly becomes important). (Edit) but of course a class helper can have a method that returns a new instance of the class (or of any other class), but that's not really a constructor. It would be a factory method. Edited April 30, 2019 by A.M. Hoornweg 1 Share this post Link to post
Uwe Raabe 2057 Posted April 30, 2019 12 minutes ago, Stefan Glienke said: Which makes sense if you think about it. Especially as one cannot override virtual methods in a class helper. 1 Share this post Link to post
David Schwartz 426 Posted April 30, 2019 Thanks, guys. After some trial-and-error, I realized a helper won't do what I was wanting it to do. So instead I created a wrapper class because I needed to add data fields as well as methods to it. That seemed easier than dealing with a derived object that's part of 3rd-party lib. Share this post Link to post
Lars Fosdal 1792 Posted May 9, 2019 Both class and record helpers could do with a real upgrade. The lack of inheritance and lack of support for Generics is crippling. 1 Share this post Link to post
Stefan Glienke 2002 Posted May 9, 2019 (edited) 4 hours ago, Lars Fosdal said: lack of inheritance Inheritance of helpers is not solving the problem at all - you then still can have the case where you would want to use methods from 2 different helpers which cannot inherit from each other. The only solution is to make not only the last visible helper usable but treat conflicting methods via overload resolution - just like extension methods in C# do. Edited May 9, 2019 by Stefan Glienke 2 Share this post Link to post
David Heffernan 2345 Posted May 9, 2019 C# has had this cracked for more than 10 years now. Why can't Delphi get it done? Share this post Link to post