Recent

Author Topic: overloading original FPC functions  (Read 4329 times)

Weiss

  • Full Member
  • ***
  • Posts: 187
overloading original FPC functions
« on: May 17, 2024, 09:03:52 pm »
what is the danger here? I recently ran into issue with name that I liked being already taken by function that does something entirely different in System unit. I tried changing names, but eventually overloaded the function, everything else did not sound right. I remember from C# I think, I could change the order of "using" claim unit, so that correct function is being called. Or was there something about appending the function with unit name. "Overload" seem to be more assertive. But with my lack of experience, I am not sure if this won't bite me later. Please share your thoughts.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 566
Re: overloading original FPC functions
« Reply #1 on: May 17, 2024, 11:11:16 pm »
The question to ask yourself, in my opinion, is "which approach is kinder to someone in the future that may need to read this code?"


Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: overloading original FPC functions
« Reply #2 on: May 17, 2024, 11:12:28 pm »
This should work

Code: Pascal  [Select][+][-]
  1. Myunit.myfunctionname  //this call your function
  2. Functionname   //this call the default functin
  3.  


Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • Censorship about opinions does not belong here.
Re: overloading original FPC functions
« Reply #3 on: May 18, 2024, 07:05:37 am »
In general you are correct to assume that unit order matters, but in your case the original function is in system and the system unit is included implicitly, so that does not help very much.
For the same reason it is not a good idea to override or overload or reintroduce functions from system if, as you state, your function has a completely different meaning: suppose you need the functionality from system? Although it will work it is not good design.
@Paolo's answer is a good solution, but it is in my opinion a better solution to rename your function.
Note that this assumes that the function from system and your own function have the exact same parameter list and result. Otherwise there is - in mode objfpc - no need, because your function would be an implicit overload.
Still, that can cause problems. Better rename.
« Last Edit: May 18, 2024, 07:25:16 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1229
Re: overloading original FPC functions
« Reply #4 on: May 20, 2024, 01:41:57 am »
I think fpc should be more strict to prevent reusing things that should be reserved words. I once used Dec as a key for resource strings and the dec() function was no longer accessible
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Weiss

  • Full Member
  • ***
  • Posts: 187
Re: overloading original FPC functions
« Reply #5 on: May 20, 2024, 08:12:54 am »
I think fpc should be more strict to prevent reusing things that should be reserved words. I once used Dec as a key for resource strings and the dec() function was no longer accessible

in my case it wouldn't be a problem, this is genuine overloading of function with different parameters. My function exists in an implementation part, not in an interface. I understand danger here is only a confusion by someone who knows what other obscure function in system unit does.

alpine

  • Hero Member
  • *****
  • Posts: 1303
Re: overloading original FPC functions
« Reply #6 on: May 20, 2024, 09:21:35 am »
Quote
Please share your thoughts.
The only justification to obscure a system function would be for debugging purposes or to make it work better while doing the same thing. The other is just aiming at your own leg.

Rename it, or put a specific suffix, or enclose it in a dedicated scope (eg. static class function) and use it qualified. https://www.freepascal.org/docs-html/ref/refsu30.html
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1229
Re: overloading original FPC functions
« Reply #7 on: May 20, 2024, 03:45:34 pm »
@weiss what was the name of the function you reused?
I think it’s a bad idea to reuse names. I’d much rather make a new name with underscores or something.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Weiss

  • Full Member
  • ***
  • Posts: 187
Re: overloading original FPC functions
« Reply #8 on: May 21, 2024, 05:38:07 am »
@weiss what was the name of the function you reused?
I think it’s a bad idea to reuse names. I’d much rather make a new name with underscores or something.

swap(). I did not have to overload, like Thaddy suggested, it works without appending " : overload". I can't remember how did I come to conclusion that I have to overload, perhaps when I typed, IDE suggested different parameters and a path to it.

Like I said in my original post, I tried renaming. It just doesn't work, when what you do is actually swapping a and b. A becomes B and B becomes A. Swap(a,b). Scalars or arrays. I eventually overloaded my swap because I needed to swap many other things. I will probably be swapping bitmaps and who knows what else. And then, there is a little obscure original "swap" which does not really swap but "flip". Hard decision. Folks, I even went to all the trouble disturbing busy people on this forum. And if all there is a bit of confusion, then I am re-using the name. There are not that many words in my vocabulary.

Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • Censorship about opinions does not belong here.
Re: overloading original FPC functions
« Reply #9 on: May 21, 2024, 07:02:47 am »
 :) Swap is indeed a good candidate.. The use case for the swap in system is not as strong as almost any other swap you can think of. And there is an alternative by using rotation.
(not to mention that it happily swaps the sign bit away, rendering it useless for signed types, you must use something like sar + xor to do that correctly)

In mode Objfpc the overload is not necessary, but it is in mode Delphi!

In general I think you just provided counter-proof against the advice to rename.
System.swap is horrible, re-use the name by all means  :D :D 8) ::)
« Last Edit: May 21, 2024, 07:38:49 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1229
Re: overloading original FPC functions
« Reply #10 on: May 21, 2024, 03:50:55 pm »
Swap is a name that doesn’t really give much information apart from a general idea of swapping. I once read a book that suggested how to name procedures. If I was swapping things I would use a name like swap_specifics
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

alpine

  • Hero Member
  • *****
  • Posts: 1303
Re: overloading original FPC functions
« Reply #11 on: May 21, 2024, 05:29:25 pm »
@weiss what was the name of the function you reused?
I think it’s a bad idea to reuse names. I’d much rather make a new name with underscores or something.

swap(). I did not have to overload, like Thaddy suggested, it works without appending " : overload". I can't remember how did I come to conclusion that I have to overload, perhaps when I typed, IDE suggested different parameters and a path to it.

Like I said in my original post, I tried renaming. It just doesn't work, when what you do is actually swapping a and b. A becomes B and B becomes A. Swap(a,b). Scalars or arrays. I eventually overloaded my swap because I needed to swap many other things. I will probably be swapping bitmaps and who knows what else. And then, there is a little obscure original "swap" which does not really swap but "flip". Hard decision. Folks, I even went to all the trouble disturbing busy people on this forum. And if all there is a bit of confusion, then I am re-using the name. There are not that many words in my vocabulary.
That changes the discussion a bit. :) Swap is a name so vague...

But I'll have to agree with Joanna this time, I'd never name a procedure in such an uninformative way. Unless is a nested one and it really doesn't matter.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: overloading original FPC functions
« Reply #12 on: May 22, 2024, 10:14:31 pm »
I think fpc should be more strict to prevent reusing things that should be reserved words.

This is not about reserved words or keywords. For those one needs to prefix them with & to be able to use them (e.g. &type or &while). Anything contained in the System unit however is not a keyword. They are just symbols and for these the normal visibility rules apply. Not to mention that FPC itself uses this functionality: Integer is declared as 16-bit signed type in the System unit, but as a 32-bit signed type in the ObjPas unit which is automatically used if mode ObjFPC or Delphi is used, thus using Integer will in fact access ObjPas.Integer unless you manually specify System.Integer. This is simply how Pascal works.

I once used Dec as a key for resource strings and the dec() function was no longer accessible

You could have just used System.Dec as is the case for any symbol hidden in this way.

 

TinyPortal © 2005-2018