Re: [fpc-pascal] implements
Am 02.09.2017 06:34 schrieb "Ryan Joseph" : > > I think I asked this some years ago but I came across it again I just don’t get what the point of this is. There is an “implements” property but it seems like yet another half-baked feature from Delphi that wasn’t implemented completely or is broken. What’s the point of implementing an interface like this on TBaseClass if you need to access all the methods by using the property name (“hook” in this case) when you could just add an instance of THook in TBaseClass? It adds so much noise and clutter in the language and for what? The only reason it makes sense is if you could call “base.DoIt” and omit the .hook every time you’re typing but that was overlooked for some reason. Why?? > > > type > IHook = interface ['IHook'] > procedure DoIt; > end; > > type > THook = class (IHook) > procedure DoIt; > end; > > procedure THook.DoIt; > begin > writeln(ClassName+' did it'); > end; > > type > TBaseClass = class (IHook) > private > m_hook: IHook; > public > property Hook: IHook read m_hook implements IHook; > constructor Create; > end; > > constructor TBaseClass.Create; > begin > m_hook := THook.Create; > end; > > > base: TBaseClass; > > base := TBaseClass.Create; > > base.DoIt; // CANT DO THIS > base.hook.DoIt; // MUST DO THIS Because you must use the interface and not the class instance: === code begin === var base: TBaseClass; hook: IHook; begin base := TBaseClass.Create; hook := base; hook.DoIt; // will call base.hook.DoIt end; === code end === Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] implements
> On Sep 2, 2017, at 2:35 PM, Sven Barth via fpc-pascal > wrote: > > Because you must use the interface and not the class instance: > > === code begin === > > var > base: TBaseClass; > hook: IHook; > begin > base := TBaseClass.Create; > hook := base; > > hook.DoIt; // will call base.hook.DoIt > end; > > === code end === > but then what is the purpose of this? It was almost a very powerful way to add methods to a class without subclassing (like class helpers) but the simple fact the “hook” identifier is not implied makes it useless. The only possible use of this I could think of is not needing to type “hook.” every time but since you need to anyways I just don’t get it. Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] implements
On 2017-09-02 08:44, Ryan Joseph wrote: but then what is the purpose of this? Please search the internet about Interfaces and probably Design Patterns too. Have ever heard the phrase: "Code to an Interface, not an Implementation". Interfaces are so much more than simply "adding methods to a class". Regards, Graeme -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] implements
> On Sep 2, 2017, at 3:27 PM, Graeme Geldenhuys > wrote: > > Please search the internet about Interfaces and probably Design Patterns too. > Have ever heard the phrase: "Code to an Interface, not an Implementation". > > Interfaces are so much more than simply "adding methods to a class". I understand interfaces just fine but I don’t understand why you want a class to appear as if it implements an interface but then having to call methods on a 2nd class. It separates the code (which is nice) but then gives you an annoying extra step of typing hook.XXX for every method. It just feels broken and not complete as a feature. Furthermore if you know that a class implements an interface but you need to call another variable (like hook) then how do you know the name of the variable??? I use interfaces often but that would break them for me in most cases. Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] implements
On 2017-09-02 09:38, Ryan Joseph wrote: It separates the code (which is nice) but then gives you an annoying extra step of typing hook.XXX for every method. I don't have that problem, and I use interfaces extensively. Like multi-threading, Interfaces are an advanced feature of the Object Pascal language. Unfortunately, many ways that it can be used incorrectly too. When I use interfaces, I work with the object instance only via an Interface reference. The interface that represents the functionality I need. I also prefer not to use reference counting with interface, but I definitely see a use for reference counting - but again, never mix the two in the same application. I also use interface delegation, which reduces code a lot. Probably the best example I can think of off the top of my head that shows Interface usage very well is a set of 8 articles (with code) implementing Model-View-Presenter (MVP). I don't know where I originally code the articles and code from, but I can email you [in private] what I have. She shows prenty of ways using interfaces. Regards, Graeme -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] implements
Am 02.09.2017 10:38 schrieb "Ryan Joseph" : > > > > On Sep 2, 2017, at 3:27 PM, Graeme Geldenhuys < mailingli...@geldenhuys.co.uk> wrote: > > > > Please search the internet about Interfaces and probably Design Patterns too. Have ever heard the phrase: "Code to an Interface, not an Implementation". > > > > Interfaces are so much more than simply "adding methods to a class". > > I understand interfaces just fine but I don’t understand why you want a class to appear as if it implements an interface but then having to call methods on a 2nd class. It separates the code (which is nice) but then gives you an annoying extra step of typing hook.XXX for every method. It just feels broken and not complete as a feature. > > Furthermore if you know that a class implements an interface but you need to call another variable (like hook) then how do you know the name of the variable??? I use interfaces often but that would break them for me in most cases. As Graeme said: the point is to code against interfaces, not classes. Don't pass around or work with the TBaseClass instance, instead only with the IHook interface. And this is what the implements feature is for. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal