params) Declaring params after the vararg vararg param as the last one in listĭeclaring vararg param as the last one in list, in a Kotlin function, looks like this: fun format(format: String, vararg params: String)Īnd will be compiled into the corresponding Java code: void String format, String. Now let’s see how the decompiled corresponding Java source code looks like when declaring vararg parameters. While in Kotlin, the vararg parameter doesn’t have to be the last one in the list, multiple vararg parameters are still prohibited. In Java, the vararg parameter has to be the last one in the parameters list - so you can only have one vararg parameter. You can do that by prefixing parameter name with the vararg modifier: fun format(format: String, vararg args: Any) vararg rules Kotlin also supports declaring a function that can have a variable number of arguments. In this article we will start by looking at the basics about vararg and spread operator, and then explore some more elaborated scenarios - while analysing what happens behind the scenes. In some cases, you might need to define a function that should have a variable number of parameters a couple of examples that you probably have encountered in Android are the printf(String format, Obj.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |