cdew / dwscript

Automatically exported from code.google.com/p/dwscript
0 stars 0 forks source link

Helpers not override parent methods #255

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Code:

Type

 TClassHelper = Helper For TObject

  Function ClassName : String;
  Begin

   Result := 'Helper.' + Self.ClassName;

  End;

 End;

PrintLn(TObject.Create.ClassName);  

Output:

TObject

Expected:

Helper.TObject

Original issue reported on code.google.com by kazantse...@mail.ru on 30 Apr 2012 at 3:18

GoogleCodeExporter commented 9 years ago
Interestingly the above code ends up in an infinite loop and a stack overflow 
in Delphi... is there a syntax to access the original method in Delphi in such 
a case? or is it hidden forever?

Currently helpers have lower priorities than "native" methods, in part because 
I didn't knew how to resolve the above ambiguity (didn't find anything in the 
doc about it).

Original comment by zar...@gmail.com on 30 Apr 2012 at 3:42

GoogleCodeExporter commented 9 years ago
Delphi solution:

 TObjectHelper = Class Helper For TObject

  Class Function ClassName : String;

 End;

 Class Function TObjectHelper.ClassName : String;
 Begin

  Result := 'Helper.' + Inherited ClassName;

 End;

Original comment by kazantse...@mail.ru on 30 Apr 2012 at 3:54

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1431.

Original comment by zar...@gmail.com on 2 May 2012 at 2:43

GoogleCodeExporter commented 9 years ago
Code:

Type

 THelper1 = Helper For TObject

  Class Function ClassName : String;
  Begin

   Result := 'Helper1.' + ClassName;

  End;

 End;

Type

 THelper2 = Helper For TObject

  Class Function ClassName : String;
  Begin

   Result := 'Helper2.' + ClassName;

  End;

 End;

PrintLn(TObject.Create.ClassName);

Output:

Helper1.TObject

Original comment by kazantse...@mail.ru on 3 May 2012 at 9:06

GoogleCodeExporter commented 9 years ago
The last snippet is about the scope & priority resolution.
If both helpers are for the same type, I would say to prioritize the last one.

But if they're for different types but both apply, what to do?
f.i Helper2 is on TObject, Helper1 on TSubObject, and you're calling them on a 
TSubObject, should Helper2 still have more priority (because it was declared 
last) or less priority (because it is less specific)?

Original comment by zar...@gmail.com on 3 May 2012 at 10:08

GoogleCodeExporter commented 9 years ago
I think, helper specialization has more priority than declaration order

Original comment by kazantse...@mail.ru on 3 May 2012 at 10:46

GoogleCodeExporter commented 9 years ago
Re-opened the issue so as not to forget about it.

Original comment by zar...@gmail.com on 3 May 2012 at 2:54