When I need a variation in procedures (standalone, not a class or object procedure) I use across my applications, I often find it more convenient to create a copy the unit into the project folder and make the changes there. Sometimes they get merged back into the shared unit or am thinking now of using classes with procedures, without any data in them, so that I subclass them in the projects where I need to change them. Are the some good examples and some known good working practices on working with procedures in this way?
Mailing lists / lazarus / 2012-04
Best practices on using classes for subroutine libraries
9 messages · 5 people · –
- FFrank Church 3
- MMarcos Douglas 3
- HHans-Peter Diettrich 1
- JJoao Morais 1
- SSven Barth 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Show quoted text 5 lines
one new implementation, that can be turned on and off for testing purposes. The $DEFINEs are set and documented near the top of the unit, as an overview of available options.
I am thinking now of using classes with procedures, without any data in them, so that I subclass them in the projects where I need to change them.
That's another useful solution.
DoDi
When I need a variation in procedures (standalone, not a class or object procedure) I use…
Frank Church wrote 10 lines
I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd etc) in static classes.
TmyFoo = class
public
class procedure Exec; static;
end;Marcos Douglas
Marcos Douglas wrote 20 lines
Do you have some examples to show how it is done?
Are there some examples in the FCL/LCL etc
Show quoted text 7 lines
Frank Church wrote 25 lines
In FCL/LCL I don't know if exists.
My example:
type
TMsg = class
public
class procedure Info(const ATxt: string); static;
end;
implementation
class procedure TMsg.Info(const ATxt: string);
begin
ShowMessage(ATxt);
end;------------------------
Using:
begin
TMsg.Info;
end;Marcos Douglas
Marcos Douglas wrote 51 lines
I see that you declare your procedure as static. Is that the way they should be or is that your own preference?
Is it possible to make them virtual and override them or replace them in descendant classes?
Show quoted text 8 lines
Frank Church wrote 57 lines
I'd say it is his own preference. Static methods are like normal, global functions/procedures except they are located inside classes. You can even pass them to non-object procedure variables. If you want to override them you should leave out the "static" and declare them "virtual" (this works with class methods as well as with normal methods).
Regards,
Sven
I see that you declare your procedure as static. Is that the way they should be or is…
Frank Church wrote 57 lines
Static methods do not have Self implicit parameter. They are like normal procedure/function.
Without 'static' works too, but the assembler generated gets better with static. To override you should leave out the 'static' to use 'virtual'.
Marcos Douglas
I see that you declare your procedure as static. Is that the way they should be or is…
Frank Church wroteIs it possible to make them virtual and override them or replace them in descendant classes?
======>>>======
{$mode objfpc}
type
tfooclass = class of tfoo;
tfoo = class
public
class function m1: string; virtual;
end;
tbar = class(tfoo)
public
class function m1: string; override;
end;
class function tfoo.m1: string;
begin
Result := 'tfoo';
end;
class function tbar.m1: string;
begin
Result := 'tbar';
end;
var
vclass: tfooclass;
begin
vclass := tfoo;
writeln('1: ', vclass.m1);
vclass := tbar;
writeln('2: ', vclass.m1);
end.======<<<======
Joao Morais
