Forum > General

Helper declaration changes behaviour

(1/2) > >>

damieiro:
Hi!

I do not know if this is a feature or a bug. I have been testing helpers, and i note that the order in which is declared, changes the behaviour.

Example


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program tohelperstest;{in this code, inherited calls tobject helper method}  uses classes, sysutils; typeTObjectHelper = class helper for TObject  public  procedure AfterConstruction; {hide TObject.Afterconstruction}  procedure BeforeDestruction;end;  procedure TObjectHelper.AfterConstruction;begin  writeln ('TOH AC:',self.classname);end; procedure TObjectHelper.BeforeDestruction;begin  writeln ('TOH BD:',self.classname);end;  typeTDescendant= class (Tobject)  public  procedure AfterConstruction;override;  procedure BeforeDestruction;override;end; procedure TDescendant.AfterConstruction;begin  inherited;  writeln ('TDe AC:',self.classname);end; procedure TDescendant.BeforeDestruction;begin  inherited;  writeln ('TDe BD:',self.classname);end; typeTDescendant2= class (TDescendant)end;   var  MyClass: TObject;  MyDescendant:TDescendant;  MyDescendant2:TDescendant2;  begin  MyClass:=TObject.Create;  MyDescendant:=TDescendant.Create;  MyDescendant2:=TDescendant2.Create;   writeln;  writeln ('Hi World');  writeln;   FreeAndNil (MyClass);  FreeAndNil (MyDescendant);  FreeAndNil (MyDescendant2);  readln; end.                                      
But if the helper is declared After the other classes declaration is ignored.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program tohelperstest;{in this code, inherited does nothing, so i guess is called former TObject methods and not the helper ones...}  uses classes, sysutils; typeTDescendant= class (Tobject)  public  procedure AfterConstruction;override;  procedure BeforeDestruction;override;end; procedure TDescendant.AfterConstruction;begin  inherited;  writeln ('TDe AC:',self.classname);end; procedure TDescendant.BeforeDestruction;begin  inherited;  writeln ('TDe BD:',self.classname);end; typeTDescendant2= class (TDescendant)end;  {declared after}typeTObjectHelper = class helper for TObject  public  procedure AfterConstruction; {hide TObject.Afterconstruction}  procedure BeforeDestruction;end;  procedure TObjectHelper.AfterConstruction;begin  writeln ('TOH AC:',self.classname);end; procedure TObjectHelper.BeforeDestruction;begin  writeln ('TOH BD:',self.classname);end;    var  MyClass: TObject;  MyDescendant:TDescendant;  MyDescendant2:TDescendant2;  begin  MyClass:=TObject.Create;  MyDescendant:=TDescendant.Create;  MyDescendant2:=TDescendant2.Create;   writeln;  writeln ('Hi World');  writeln;   FreeAndNil (MyClass);  FreeAndNil (MyDescendant);  FreeAndNil (MyDescendant2);  readln; end.                                      

damieiro:
Even an intercalate declaration changes behaviour

TDescendant doesn't go in helper scope, but TDescendant2, yes. I do not know it this is intended to work this way


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program tohelperstest; uses classes, sysutils;  typeTDescendant= class (Tobject)  public  procedure AfterConstruction;override;  procedure BeforeDestruction;override;end; procedure TDescendant.AfterConstruction;begin  inherited;  writeln ('TDe AC:',self.classname);end; procedure TDescendant.BeforeDestruction;begin  inherited;  writeln ('TDe BD:',self.classname);end; typeTObjectHelper = class helper for TObject  public  procedure AfterConstruction; {hide TObject.Afterconstruction}  procedure BeforeDestruction;end;  procedure TObjectHelper.AfterConstruction;begin  writeln ('TOH AC:',self.classname);end; procedure TObjectHelper.BeforeDestruction;begin  writeln ('TOH BD:',self.classname);end;   typeTDescendant2= class (Tobject)  public  procedure AfterConstruction;override;  procedure BeforeDestruction;override;end; procedure TDescendant2.AfterConstruction;begin  inherited;  writeln ('TDe AC:',self.classname);end; procedure TDescendant2.BeforeDestruction;begin  inherited;  writeln ('TDe BD:',self.classname);end;  var  MyClass: TObject;  MyDescendant:TDescendant;  MyDescendant2:TDescendant2;  begin  MyClass:=TObject.Create;  MyDescendant:=TDescendant.Create;  MyDescendant2:=TDescendant2.Create;   writeln;  writeln ('Hi World');  writeln;   FreeAndNil (MyClass);  FreeAndNil (MyDescendant);  FreeAndNil (MyDescendant2);  readln; end.   

Thaddy:
The helper needs always be the LAST in scope, not only in a single program, but also in unit order. This is documented.
[edit] but even if I change it to a proper order there is something fishy going on....

damieiro:
Sorry for my bad english (i'm not english native speaker/writer)...

But if Last in scope, it would be ignored as i point in my example (first post). (Last scope, last declaration or am i understanding wrong?)

I only see that the order in which is processed is when taken into account (second post). I do not like that behaviour because i think it can be a source of errors. (well, i disklike helpers.). Just for testing..

Thaddy:
No, you understand correctly: it should work as you expect, but it doesn't in a simple program.
If I can determine it is a real bug, I will report it.

Navigation

[0] Message Index

[#] Next page

Go to full version