Is it possible to have a default argument for a fu...
# getting-started
i
Is it possible to have a default argument for a function parameter (function having another function as a parameter and that function parameter has a default value just as a variable could have)?
o
Hello. Yes, it is possible. For example, you will use somethings like:
Copy code
fun doExample(exampleSubFun: (TypeOne, TypeTwo) -> Unit = {})
b
Copy code
fun test(block: () -> Unit = { println("default") }) {

}
i
Okay. And how would I reference a number for example.
dip:(Int) -> Int = { TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, [WHAT EVER INT NUM I SEND].toFloat(), resources.displayMetrics).toInt() }
Should I do
dip:(nameOfTheVariable: Int) -> Int
b
For single-arg functions use
it
Copy code
fun t(dip: (Int) -> Int = { TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, it.toFloat(), resources.displayMetrics).toInt() }) {}
Otherwise
Copy code
fun t(dip: (Int,Int) -> Int = {int1,int2 -> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, int1.toFloat(), resources.displayMetrics).toInt() }) {}
i
Thanks!