Is it possible to write a function with varargs th...
# announcements
n
Is it possible to write a function with varargs that calls a Java function with varargs of type
Object
? Say, I want a shorter name for Android’s `getString()`:
Copy code
@NonNull
    public final String getString(@StringRes int resId, Object... formatArgs) {
        return getResources().getString(resId, formatArgs);
    }
How do I write a Kotlin function that just forwards its arguments to this
getString
? According to the docs, I should use
kotlin.Any!
Android Studio does not even let me add the !. Using just
vararg Any
results in
IllegalFormatExceptions
s
A type ending in
!
is a platform type. It comes in from Java. You can never define one yourself. Have you tried the spread operator?
*formatArgs
? (note the
*
)
n
Doesn’t this imply that I have to pass an array? So, I need to wrap my vararg params into an array?
s
Copy code
fun Resources.myGetString(id: Int, vararg arguments: Any) =
    this.getString(id, *arguments)
👍 1
(where
Resources
is the Android Resources class defined in Java)
n
Thank you. Works fine.
👍 1
l
I published what you're looking for in Splitties, it's called `str`: https://github.com/LouisCAD/Splitties/tree/master/modules/resources#text
👍 1