Reputation: 8960
In a Kotlin app, I have this method:
fun <VALUE> metadataOf(vararg pairs: Pair<String, VALUE>) =
MetaData.from(pairs.toMap())!!
Which I'm then using like:
metadataOf(
"sId" to message.sId,
"userId" to message.userId
)
I'm trying to write a method which can create the above for me from message
- however I'm not sure how to return a list of pairs - this is what I put together based on the input parameter from metadataOf(vararg pairs: Pair<String, VALUE>)
fun metadataFrom( message: CommandMessage<Any> ): Pair<String, Any> {
return (
"sId" to message.sId,
"userId" to message.userId
)
}
Upvotes: 0
Views: 612
Reputation: 16224
You have two choices here.
List
:fun metadataFrom(message: CommandMessage<Any>): List<Pair<String, Any>> = listOf(
"sId" to message.sId,
"userId" to message.userId
)
You can use it in this way:
val result = metadataFrom(message)
metadataOf(*result.toTypedArray())
Array
:fun metadataFrom(message: CommandMessage<Any>): Array<Pair<String, Any>> = arrayOf(
"sId" to message.sId,
"userId" to message.userId
)
You can use it in this way:
val result = metadataFrom(message)
metadataOf(*result)
The second is more performant since you are directly creating the array used as input for metadataOf
but nothing noticeable if you haven't a huge amount of data. So choose your favorite one.
Upvotes: 2