Jump to content
Sign in to follow this  
pyscripter

Class helpers catch

Recommended Posts

I would like to share the following in case you encounter the same issue.

 

Class and Record Helpers (Delphi) - RAD Studio states that:

Quote

You can define and associate multiple helpers with a single type. However, only zero or one helper applies in any specific location in source code. The helper defined in the nearest scope will apply. Class or record helper scope is determined in the normal Delphi fashion (for example, right to left in the unit's uses clause).

 

Actually, this is not entirely correct.  Consider the following:

 

Unit HelperUnit1.pas:

TObjectHelper1 = class helper for TObject
  procedure Test;
end;

 

Unit2 HelperUnit2.pas:

TObjectHelper2 = class helper for TObject
  procedure Test;
end;

Unit SupportClasses,pas:

uses
   HelperUnit1;
type
  TMyClass: class
  end;  

Unit MainUnit.pas

interface
implementation
uses
  SupportClasses, HelperUnit2;
begin
  var MyClass:= TMyClass.Create;
  MyClass.Test;
end;

MyClass.Test will use the HelperUnit1.TObjectHelper1.Test implementation even if HelperUnit1 is not even in scope, let alone being "in nearest scope".

 

So it appears that if a class helper is in scope where a class is defined, it is used unconditionally in all units of a project.  If not, then what it is stated in the documentation applies.

Edited by pyscripter
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×