I'm gonna write a method like:
object UIBehavior {
fun dialog(context: Context, title: Int | String, message: Int | String){
val dialogObj = AlertDialog.Builder(context)
dialogObj.setTitle(title)
dialogObj.setMessage(message)
}
}
The methods dialogObj.setTitle and dialogObj.setMessage allow two types of parameters, and how can I delare the parameter that can let the method dialog allow only two types Int and String?
To declare a string in Kotlin, we need to use double quotes(” “), single quotes are not allowed to define Strings.
Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.
val and var both are used to declare a variable. var is like general variable and it's known as a mutable variable in kotlin and can be assigned multiple times. val is like Final variable and it's known as immutable in kotlin and can be initialized only single time.
There are different data types in Kotlin:Integer Data type. Floating-point Data Type. Boolean Data Type.
You can't do that in Kotlin.
But you can have multiple versions of a function, e.g.
object UIBehavior {
fun dialog(context: Context, titleId: Int, messageId: Int){
val titleString = context.getString(titleId)
val messageString = context.getString(messageId)
dialog(context, titleString, messageString)
}
fun dialog(context: Context, title: String, message: String) {
val dialogObj = AlertDialog.Builder(context)
dialogObj.setTitle(title)
dialogObj.setMessage(message)
}
}
That way you can simply call the function with either ids or strings and it looks like you are using the same function
UIBehavior.dialog(this, R.string.title, R.string.message)
UIBehavior.dialog(this, "title", "message")
You could also use a common supertype of Int
and String
but that would allow a lot more and I wouldn't recommend that.
fun dialog(context: Context, title: Any, messageId: Any){
val titleString = when (title) {
is String -> title
is Int -> context.getString(title)
else -> throw IllegalArgumentException("Unsupported type")
}
val messageString = when ...
...
dialog(context, titleString, messageString)
}
Generics don't work here either because you can't call dialogObj.setTitle(title)
dynamically. It must be known at compile time whether you want to call the Int
or the String
overload of that function. It's also not really different from using Any
.
fun <T>dialog(context: Context, title: T, message: T){
if(title !is String || title !is Int) throw InvalidParameterException()
val dialogObj = AlertDialog.Builder(context)
dialogObj.setTitle(title)
dialogObj.setMessage(message)
}
fun dialog(context: Context, title: Any, message: Any){
if(title !is String || title !is Int) throw InvalidParameterException()
val dialogObj = AlertDialog.Builder(context)
dialogObj.setTitle(title)
dialogObj.setMessage(message)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With