Default parameters
Sometimes, it is convenient to provide default values for parameters in a function. Let's say we want to create a thread pool. The parameter to set the number of threads could default to the number of CPU cores. This would be a sensible default, but the user might still want to use something different.
The way to achieve this in languages without default parameters is to offer overloaded versions of the same function:
fun createThreadPool(): ExecutorService { val threadCount = Runtime.getRuntime().availableProcessors() return createThreadPool(threadCount) } fun createThreadPool(threadCount: Int): ExecutorService { return Executors.newFixedThreadPool(threadCount) }
Here, the user can now choose which version to invoke. However, sometimes the number of parameters means that we end up with many overloaded variations of the same function, resulting in needless boilerplate. For example, the Java...