本文介绍了Kotlin-配对界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kotlin的新手,不确定如何抽象以下内容.

I'm new to Kotlin, and unsure how to abstract the following.

例如,我有这个:

metadataOf(
   "sId" to "123",
   "uId" to "456"
)

其中metadataOf()看起来像这样

fun <VALUE> metadataOf(vararg pairs: Pair<String, VALUE>) =
    MetaData.from(pairs.toMap())!!

我想要那个

metadataOf(
   "sId" to "123",
   "uId" to "456"
)

要标准化,例如说metadata.messagemetadataFrom(message)这样的东西会为我产生这两对. (当然,将来如果我添加更多,可以很容易地在一个地方添加)

To be standarised, so say something like metadata.message or metadataFrom(message) would produce those 2 pairs for me. (And of course in the future if I added more and I could easily do so in one place)

我该如何写呢?

任何帮助表示赞赏.

推荐答案

听起来像您真正想要的是:

Sounds like what you really wanted is:

data class SMetadata(val sId: String, val uId: String)

//fun metadataOf(vararg sMetadatas: SMetaData) =
//    MetaData.from(sMetaDatas.map { mapOf("sId" to it.sId, "uId" to it.uId) })

//metadataOf(
//   SMetaData(sId = "123", uId = "456")
//)

编辑:显然您真正想要的只是

apparently all you really wanted was

fun SMetaData.toMetaData() = MetaData.from(mapOf("sId" to sId, "uId" to uId))

这篇关于Kotlin-配对界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 19:46