Kotlin asterisk operator before variable name or Spread Operator in Kotlin

后端 未结 5 705
猫巷女王i
猫巷女王i 2020-11-30 00:59

I want to know what exactly an asterisk does before a variable name in Kotlin. I saw this (*args) in a Spring boot Kotlin example:

@SpringBootApp         


        
5条回答
  •  执念已碎
    2020-11-30 01:34

    In addition to the answers that were directly towards "what is this thing!?!", you often have the case where you have a List and want to pass it to a function that is expecting a vararg. For this, the conversion is:

    someFunc(x, y, *myList.toTypedArray())
    

    Assuming that last parameter of someFunc is vararg of the same type as the elements in the list.

提交回复
热议问题