Here are some reasons why you might want this https://en.m.wikipedia.org/wiki/Ad_hoc_polymorphism
Re:Type Polymorphic Functions in TypeScript
by pc86 on Wednesday March 16, 2022 @12:14AM
(#30695246)
There are probably better ways to achieve this than some pseudo-Hungarian notation on method signatures, right?
I'm predisposed to agree with you but I would hate looking at code and seeing method signatures like DoThingString(string), DoThingInt(int), etc.
Re:Type Polymorphic Functions in TypeScript
by galaxyLogic on Wednesday March 16, 2022 @04:55AM
(#30696645)
Those look clumsy but you would only use something like that when in fact you define a similar but not the same operation for Strings and Integers. And then the argument-type-suffix would look more natural. For instance:
saveInt (i)
saveString(s)
saveBox(box)
saveCat (cat)
And then it might make more sense to define classes Box and Cat and say: box.save()
cat.save()
Re:Type Polymorphic Functions in TypeScript
by anoctopus on Wednesday March 16, 2022 @12:52AM
(#30695460)
Parametric polymorphism is great and undervalued by most languages, but ad hoc polymorphism is also valuable at times, mainly when the polymorphic function represents the same abstract operation on distinct types.
A more readable solution I think is to define multiple functions whose names differ by a suffix that indicates argument type.
Arguments with default values are not really "polymorphic". They juts make it possible to call the function with fewer arguments.
And instead of "polymorphic functions" why not use ES6 classes which are polymorphic by their nature. The class of the recipient determines which method gets executed. There too you can use the approach of defining multiple similar methods whose name differs by a suffix indicating their argument-type.