Sign in Join

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

Mailing lists / fpc-pascal / 2013-10

Helpers: Implementation Location influencing the result

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Daniel Gaspary

I have created an example to show the different results I got due to where the implementation of a object is .

The code is at http://pastebin.com/wY2qmZFm

If you cut and paste the code block at "Location A" to the "Location B" the result of the program changes.

At Location A the method of the helper is used. At Location B it is not.

My questions are

Is this right?

Why?

Thank you.

Sven Barth
to Daniel Gaspary
"Daniel Gaspary" wrote 14 lines
I have created an example to show the different results I got due to where the implementation of a object is . The code is at http://pastebin.com/wY2qmZFm If you cut and paste the code block at "Location A" to the "Location B" the result of the program changes. At Location A the method of the helper is used. At Location B it is not. My questions are Is this right?

Yes.

Why?

At location A the helper is not yet declared, thus GetAnotherString uses the GetString function of its own class. On location B the helper is already declared and thus the implementation of GetAnotherString will pick up the helper's GetString function instead.

As normally helpers aren't declared in the same unit as their extended type this isn't necessarily that obvious. ;)

Regards,
Sven

Daniel Gaspary
to Sven Barth
Sven Barth wrote
At location A the helper is not yet declared, thus GetAnotherString uses the GetString function of its own class. On location B the helper is already declared and thus the implementation of GetAnotherString will pick up the helper's GetString function instead.

I believed that the location that matters, in this case, was only the object instantiation (program/implementation body), because it was, in both cases, after the helper declaration. A little weird.

Thanks, Sven.

Sven Barth
to Daniel Gaspary
Daniel Gaspary wrote 9 lines
On Sat, Oct 5, 2013 at 8:27 PM, Sven Barth wrote: > At location A the helper is not yet declared, thus GetAnotherString uses the > GetString function of its own class. On location B the helper is already > declared and thus the implementation of GetAnotherString will pick up the > helper's GetString function instead. I believed that the location that matters, in this case, was only the object instantiation (program/implementation body), because it was, in both cases, after the helper declaration. A little weird.

No. Whenever you call a method (either inside a class using (implicit) Self or outside using a variable) the compiler checks whether a helper for that type is in scope and uses that if it is. It has nothing to do with object instantiation (that would be more for Traits/Mixins: http://en.wikipedia.org/wiki/Trait_%28computer_programming%29 ).

Regards,
Sven