Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-04

Best practices on using classes for subroutine libraries

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church

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?

Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Hans-Peter Diettrich
to Frank Church
Show quoted text 5 lines
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 use conditional compilation for such variations. Typically only for

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

Reply by email

Marcos Douglas
to Frank Church: When I need a variation in procedures (standalone, not a class or object procedure) I use…
Frank Church wrote 10 lines
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 not at all. 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. Are the some good examples and some known good working practices on working with procedures in this way?

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

Reply by email

Frank Church
to Marcos Douglas
Marcos Douglas wrote 20 lines
On Tue, Apr 10, 2012 at 1:07 AM, Frank Church wrote: > 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 not at all. > > 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. Are the some good examples and some known good working > practices on working with procedures in this way? I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd etc) in static classes. TmyFoo = class public ?class procedure Exec; static; end;

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
Marcos Douglas -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Marcos Douglas
to Frank Church
Frank Church wrote 25 lines
On 10 April 2012 12:28, Marcos Douglas wrote: > On Tue, Apr 10, 2012 at 1:07 AM, Frank Church wrote: >> 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 not at all. >> >> 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. Are the some good examples and some known good working >> practices on working with procedures in this way? > > I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd > etc) in static classes. > > TmyFoo = class > public > ?class procedure Exec; static; > end; > Do you have some examples to show how it is done? Are there some examples in the FCL/LCL etc

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

Reply by email

Frank Church
to Marcos Douglas
Marcos Douglas wrote 51 lines
On Tue, Apr 10, 2012 at 12:49 PM, Frank Church wrote: > On 10 April 2012 12:28, Marcos Douglas wrote: >> On Tue, Apr 10, 2012 at 1:07 AM, Frank Church wrote: >>> 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 not at all. >>> >>> 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. Are the some good examples and some known good working >>> practices on working with procedures in this way? >> >> I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd >> etc) in static classes. >> >> TmyFoo = class >> public >> ?class procedure Exec; static; >> end; >> > > Do you have some examples to show how it is done? > > Are there some examples in the FCL/LCL etc 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;

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
Marcos Douglas -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Sven Barth
to Frank Church
Frank Church wrote 57 lines
On 10 April 2012 19:08, Marcos Douglas wrote: > On Tue, Apr 10, 2012 at 12:49 PM, Frank Church wrote: >> On 10 April 2012 12:28, Marcos Douglas wrote: >>> On Tue, Apr 10, 2012 at 1:07 AM, Frank Church wrote: >>>> 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 not at all. >>>> >>>> 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. Are the some good examples and some known good working >>>> practices on working with procedures in this way? >>> >>> I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd >>> etc) in static classes. >>> >>> TmyFoo = class >>> public >>> class procedure Exec; static; >>> end; >>> >> >> Do you have some examples to show how it is done? >> >> Are there some examples in the FCL/LCL etc > > 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; > 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?

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

Reply by email

Marcos Douglas
to Frank Church: I see that you declare your procedure as static. Is that the way they should be or is…
Frank Church wrote 57 lines
On 10 April 2012 19:08, Marcos Douglas wrote: > On Tue, Apr 10, 2012 at 12:49 PM, Frank Church wrote: >> On 10 April 2012 12:28, Marcos Douglas wrote: >>> On Tue, Apr 10, 2012 at 1:07 AM, Frank Church wrote: >>>> 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 not at all. >>>> >>>> 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. Are the some good examples and some known good working >>>> practices on working with procedures in this way? >>> >>> I do this. Sometimes I prefer encapsulate subroutines (LCL, RTL, 3rd >>> etc) in static classes. >>> >>> TmyFoo = class >>> public >>> ?class procedure Exec; static; >>> end; >>> >> >> Do you have some examples to show how it is done? >> >> Are there some examples in the FCL/LCL etc > > 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; > 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?

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

Reply by email

Joao Morais
to Frank Church: I see that you declare your procedure as static. Is that the way they should be or is…
Frank Church wrote
Is 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

Reply by email