Recent

Author Topic: problem with calling virtual methods?  (Read 3905 times)

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
problem with calling virtual methods?
« on: August 22, 2020, 10:42:46 pm »
pas2js source used is latest trunk pulled 8/22/2020

Trying to implement some classes with pas2js and am noticing an issue with virtual methods. my example is

parent class defined with a virtual method with no implementation
Code: Pascal  [Select][+][-]
  1. TNyxElementBaseImpl
  2. ...
  3. //declaration
  4. procedure DoUpdateHeight; virtual;
  5. ...
  6. //implementation
  7. procedure TNyxElementBaseImpl.DoUpdateHeight;
  8. begin
  9.   //nothing in base
  10. end
  11.  

this parent class, makes a call to the virtual method in this other method,
Code: Pascal  [Select][+][-]
  1. procedure TNyxElementBaseImpl.NotifyHeight(const AType: TPropertyUpdateType;
  2.   const ASize: INyxSize; const AProperty: TSizeProperty);
  3. begin
  4.   if AType = puAfterUpdate then
  5.     DoUpdateHeight; //  <---------right here
  6. end;
  7.  

now, I have a child class which overrides the DoUpdateHeight method here
Code: Pascal  [Select][+][-]
  1. TNyxElementBrowserImpl = class(TNyxElementBaseImpl, INyxElementBrowser)
  2. ...
  3. //interface
  4. procedure DoUpdateHeight; override;
  5. ...
  6. //impl
  7. procedure TNyxElementBrowserImpl.DoUpdateHeight;
  8. begin
  9.   inherited DoUpdateHeight; // <------- we never get here in the debugger
  10.   UpdateSize;
  11. end;
  12.  

What should be happening is the TNyxElementBrowserImpl.DoUpdateHeight should be used, not the parent's empty stubbed method... but when debugging, the parent method is triggered rather than the overridden child.

Has anyone run into this or spot something that I may be doing improperly?
any help would be appreciated.

for reference, the full source for the examples are found at these lines,
parent:
https://github.com/mr-highball/nyx/blob/master/src/nyx.types.pas#L262
https://github.com/mr-highball/nyx/blob/master/src/nyx.types.pas#L1726

child:
https://github.com/mr-highball/nyx/blob/master/src/nyx.element.browser.pas#L72
https://github.com/mr-highball/nyx/blob/master/src/nyx.element.browser.pas#L149

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: problem with calling virtual methods?
« Reply #1 on: August 23, 2020, 11:52:41 am »
It shouldn't be, but did you tried to add SELF. to the calls?  Maybe the transpiler is forgetting to add this. to it and it may mess up the thing (never used pas2js though).

BTW, this is to push up this message so more people read it. ;)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: problem with calling virtual methods?
« Reply #2 on: August 23, 2020, 09:20:49 pm »
thanks for replying (and for the "bump") @Ñuño_Martínez, I hadn't thought of trying that, but making the change to Self.DoUpdateHeight still seems to be calling the parent stub method rather than the overridden one

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: problem with calling virtual methods?
« Reply #3 on: August 23, 2020, 10:10:54 pm »
I created this issue in mantis with a test scenario project... but tbh I couldn't break it in the same way that I'm seeing, and have no idea what to try next, just posting this for transparency (and the chance someone spots something I'm not)
https://bugs.freepascal.org/view.php?id=37633

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: problem with calling virtual methods?
« Reply #4 on: August 24, 2020, 11:38:05 am »
Are you sure you're creating the object with the correct class?  That question seems silly but it is a quite common mistake (I did it no long ago :-[).
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: problem with calling virtual methods?
« Reply #5 on: August 24, 2020, 03:35:55 pm »
Yeah, to make sure I did a WriteLn of the classname and it indeed was the child class, but the parent method is being usee (very weird to try and track down)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: problem with calling virtual methods?
« Reply #6 on: August 25, 2020, 02:15:20 pm »
Quite weird. Anyway, I had similar bugs and most times the problem was that I committed a very silly mistake, so silly it is hidden in front of you. %)

Let's hope somebody can help.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

 

TinyPortal © 2005-2018