how to set minimum of passed parameters number to a function in kotlin

Viewed 117
fun TextView.setShadowDetails(shadowColor : Int? = null, shadowDx : Float? = null, shadowDy: Float? = null , shadowRadius : Float? = null){
        setShadowLayer(shadowRadius ?: getShadowRadius(),shadowDx ?: getShadowDx(),shadowDy ?: getShadowDy(),shadowColor ?: getShadowColor())
    }

Is there a way to get a build error if no parameter is passed to this function? That is, can the minimum number of parameters transferred to this function be set to one? (In some cases I may need to access at least two parameters)

If that's possible, Kotlin is truly a wonderful language!

Tip :
Please only notify me if you have a answer other than function overloading .

1 Answers

One solution would be simple overloading, like you could do in Java as well. So not sure if you're interessted in this,a s it's no fancy Kotlin feature.

Possible solution:

fun doSomething(number1: Int, number2: Int? = null, text: String? = null) {
    //do sth
}


fun doSomething(number1: Int? = null, number2: Int, text: String? = null) {
    //do sth
}

fun doSomething(number1: Int? = null, number2: Int? = null, text: String) {
    //do sth
}

doSomething(1)

so here I overload the function and always have one parameter as nonnull. This way you're not able to call the function without any parameter.

Related