Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Function Overload and Named Arguments

  1. Function overload might cause tricky invoking bugs if you change the signature of an overloaded function. You should always be careful when you change the signature of an overloaded function. There is a graceful way of resolving this kind of issues in Kotlin (and similarly in Scala and Python), which is to use named arguments. If you invoke an function with named arguments, it is relatively robust to change of order of arguments and it fails fast if you change the names of arguments.

import kotlin.math.pow

fun myPower(base: Double, exponent: Double): Double {
    return base.pow(exponent)
}
null
myPower(2.0, 3.0)
8.0
myPower(base=2.0, exponent=3.0)
8.0
myPower(exponent=3.0, base=2.0)
8.0

Default Values of Parameters

import kotlin.math.pow

fun myPower2(base: Double, exponent: Double = 2.0): Double {
    return base.pow(exponent)
}
null
myPower2(3.0)
9.0