2

I've heard that changing a function's behaviour based on argument type is called ad hoc polymorphism:

program Adhoc;

function Add(x, y : Integer) : Integer;
begin
    Add := x + y
end;

function Add(s, t : String) : String;
begin
    Add := Concat(s, t)
end;

begin
    Writeln(Add(1, 2));                   (* Prints "3"             *)
    Writeln(Add('Hello, ', 'World!'));    (* Prints "Hello, World!" *)
end.

Is there a specific term for changing a function's behaviour based on the number of arguments passed to it?

E.g. the popular JavaScript library jQuery has many functions that do different things based on how many arguments they are passed:

$(element).attr(attribute, value)  // sets element's attribute to value
$(element).attr(attribute)  // returns element's attribute

Is this also polymorphism?

And does it have a more specific name?

(I want to use this word to talk about JS)

15
  • Normally, this would be called operator overloading. using the same name in class for different types / argument counts. It isn't polymorphism where a a different implementation is substituted for a specific method signature.
    – Kristian H
    Commented Jan 11, 2018 at 15:58
  • 7
    Ad hoc polymorphism is also known as function overloading. Behavior changes based on either arity (number of arguments) or data types. Commented Jan 11, 2018 at 16:01
  • 2
    If I had my druthers this would be call "the kind of polymorphism that doesn't really matter".
    – JimmyJames
    Commented Jan 11, 2018 at 16:16
  • @CandiedOrange so function overloading == ad hoc polymorphism == what I've described? So I've described ad hoc polymorphism?
    – minseong
    Commented Jan 11, 2018 at 16:22
  • 1
    Those are the closest words to what you describe that I know. If you want a word that excludes type and only includes arity I don't know it. Commented Jan 11, 2018 at 16:30

2 Answers 2

1

You question involves one language that supports overloading (the first one) and another that doesn't (JavaScript). That's going to make a succinct answer more difficult.

JavaScript doesn't do overloading. Not only doesn't it do overloading but it doesn't do typed parameters! It also doesn't do typed return values! A function can return nothing or something, dynamically, as it sees fit. There is virtually no compile time handling of parameter count, parameter types, and return type.

(TypeScript and/or other adds on top of JavaScript, to get the benefits of a static type system, so typos can be detected at compile time instead of by debugging.)

In JavaScript we can dynamically check the number of parameters passed, and/or check the dynamic types of the parameters, and/or see if an optional parameter was passed or not. And we can change the behavior one way or the other based on that.

the popular JavaScript library jQuery has many functions that do different things based on how many arguments they are passed

This mechanism here is optional parameters and optional return values, and programmers are varying behavior based on optional parameters presence or absence. We might call it sloppy polymorphism if we need a formal name for it. (It is not necessarily the authors of jQuery that are being sloppy but that this language offers optional parameters & optional return value as a substitute for overloading.)

This wouldn't work in some other languages: in general functions must return values and procedures cannot (aka void functions or methods) — we cannot dynamically change that. Since the return value is typed, you can't use an optional parameter to change the nature of the method (from function to procedure).

Further in other languages, optional parameters are passed as default value, which sometimes cannot be distinguished from not having been passed.

However, in some of those other languages, overloading is supported, and thus, functions with the same name and different count or types of parameters can coexist. These languages are typically doing compile-time selection (binding) of the appropriate overload to invoke, which is called ad-hoc polymorphism. I think that name is a bit too grand for what is just (compile-time) overloading; I don't see polymorphism of objects here, just two different functions that are available to choose from at compile time.

We should also mention that some languages support Variadic Functions (aka varargs), which allows a list or sequence of additional arguments. Usually at most one such list of additional args is allowed, and that list is positioned after any regular (non-varying) parameters. Typically, all the parameters in such varargs list share some common qualities, so can be thought of as a list of some type. This feature does not necessarily play well with optional parameters.

0

Having multiple functions, that vary either in their arity (number of parameters) or the types of those parameters is indeed ad hoc polymorphism and is also known as function/method overloading.

However,"changing a function's behaviour based on the number of arguments passed to it" is not polymorphism, as you only have one function and that function is switching its behaviour based on its parameters. It's often a code smell: having a function do multiple things based on its parameters is generally frowned upon.

However, with dynamic languages, like JavaScript, that do not support ad hoc polymorphism, using parameters in this way is widely recognised as an acceptable way of simulating function overloading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.