Thank you very much for your reply.
My use case is the following:
I am creating a utility library for android, in some cases I need to receive text values to display in views. To facilitate the process, I want to admit a string directly, which is the one that it would show to the user, but I also want the possibility of giving me the ID of the resource (which is integer) to obtain the string of the resources within the library.
For Example:
class DemoView {
var name: Any = "Example"
}
In this case, the user can assign as a string, so I use it directly, but if I assign it as Int I get the value of the resources:
fun show() {
val finalName = when(name){
is String -> name
else -> context.getString(name)
}
}
The downside is that Any will accept any other type of data, and I would like to restrict it to String and Int.
Another approach that I have thought of is the following
class DemoView {
private var name: Any = "Example"
fun setName(value: String) {
name = value
}
fun setName(value: Int) {
name = context.getString(value)
}
}
I don’t know if these approaches are the best, or if it can be done in a better way.