Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Default argument
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Overloaded methods== Some languages, such as [[Java (programming language)|Java]], do not have default arguments. However, the same behaviour can be simulated by using [[method overloading]] to create overloaded methods of the same name, which take different numbers of arguments; and the versions with fewer arguments simply call the versions with more arguments, with the default arguments as the missing arguments: <syntaxhighlight lang="java"> int MyFunc(int a, int b) { return MyFunc(a, b, 12); } int MyFunc(int a, int b, int c) { /* main implementation here */ } </syntaxhighlight> However, in addition to [https://dzone.com/articles/functional-default-arguments several other disadvantages], since the default arguments are not modeled in the type system, the type of a callback (aka higher-order function) can't express that it accepts either of the overloads nor simulate the default arguments with overloaded functions. Whereas, [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Default_parameters#Description in JavaScript] the non-overloaded function definition can substitute the default when the input value is <code>undefined</code> (regardless if it was implicitly <code>undefined</code> via the argument's absence at the call site or an explicitly passed <code>undefined</code> value); which is modeled as an optional argument parameter type <code>?:</code> [http://www.typescriptlang.org/docs/handbook/functions.html#optional-and-default-parameters in TypeScript]. JavaScript's solution is [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Default_parameters#Evaluated_at_call_time not resolved statically] (i.e. not at compile-time, which is why TypeScript models only the optionality and not the default values in the function's type signature) thus incurs additional runtime overhead, although it does provide more flexibility in that callbacks can independently control their defaults instead of centrally dictated by the (callback's type signature in the) type signature of the function which inputs the callback. The TypeScript solution can be [https://stackoverflow.com/questions/965690/java-optional-parameters/12994104#12994104 simulated in Java] with the <code>Optional</code> type except the analogous of an implicit <code>undefined</code> for each absent argument is an explicit <code>Optional.<Integer>absent()</code> at the call site.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)