英文:
Indexed access operator overload for a MutableList member in Kotlin
问题
以下是翻译好的内容:
这是我的情景,我有几个 MutableList
成员,第一个用于存储 String
(称为 sl),其余用于存储 Any
(称为 objects、names 等),我需要通过两种方式访问 objects、names 和其他成员中的值:
- 通过索引访问 sl,
- 通过存储在 sl 中的字符串值访问 objects、names 和其他成员,
也许一个示例会更清楚:
var tmpListItem = myStringList.sl[0] // 只读访问 sl 列表
myStringList.objects["keyname"] = "keyvalue" // 对其他成员进行读写操作
var tmpObj = myStringList.objects["keyname"]
myStringList.names["keyname"] = "keyvalue"
var tmpName = myStringList.names["keyname"]
我一直在尝试重载 MutableList
的索引访问运算符,但是我得到了这个错误:
类型不匹配:推断的类型为 String,但预期为 Int
到目前为止,这是我尝试的代码:
data class StringList(val str: String) {
val sl = mutableListOf<String>()
val objects = mutableListOf<Any?>()
val names = mutableListOf<Any?>()
val values = mutableListOf<Any?>()
val strings = mutableListOf<Any?>()
public operator fun <E> MutableList<E>.set(k: String, value: Any?) {
val t = sl.indexOf(k)
objects[t] = value
}
operator fun <E> MutableList<E>.get(e: String): Any? {
val t = sl.indexOf(e)
return objects[t]
}
}
不确定是否有其他更适合我情景的实现方法,欢迎任何建议。
英文:
This is my scenario, I have several MutableList
members, the first for storing String
(called sl) and the rest for Any
(called objects, names, ...), and I need to access the values in the objects, names and the rest through 2 ways:
- sl through the index,
- objects, names and the rest through the string value stored in sl,
Maybe an example is more clear:
var tmpListItem = myStringList.sl[0] // read only for the sl list
myStringList.objects["keyname"] = "keyvalue" // write and read for the rest
var tmpObj = myStringList.objects["keyname"]
myStringList.names["keyname"] = "keyvalue"
var tmpName = myStringList.names["keyname"]
I have been trying to overload the indexed access operator for a MutableList
, but I get this error:
> Type mismatch: inferred type is String but Int was expected
This is my attempt so far:
data class StringList(val str: String) {
val sl = mutableListOf<String>()
val objects = mutableListOf<Any?>()
val names = mutableListOf<Any?>()
val values = mutableListOf<Any?>()
val strings = mutableListOf<Any?>()
public operator fun <E> MutableList<E>.set(k: String, value: Any?) {
val t = sl.indexOf(k)
objects[t] = value
}
operator fun <E> MutableList<E>.get(e: String): Any? {
val t = sl.indexOf(e)
return objects[t]
}
}
Not sure if another implementation is more appropriate for my scenario, any suggestion is welcome.
答案1
得分: 1
如果您想从外部访问它们,那些 operator fun
不应该是 MutableList
的扩展。
data class StringList(val str: String) {
val sl = mutableListOf<String>()
val objects = mutableListOf<Any?>()
operator fun set(k: String, value: Any?) {
val t = sl.indexOf(k)
objects[t] = value
}
operator fun get(e: String): Any? {
val t = sl.indexOf(e)
return objects[t]
}
}
现在您可以这样使用它们:
myStringList["keyname"] = "keyvalue"
var tmp = myStringList["keyname"]
细节
这里有一些我想指出的事情:
-
在类内部声明的扩展函数只能在该类或其子类中访问,因此只有在
StringList
中调用该操作符函数时,您才能继续使用您的版本(例如myStringList.objects["keyname"]
)。 -
我认为您应该处理
indexOf()
返回-1
的情况,否则set
和get
函数将抛出越界异常。 -
如果可能的话,考虑使用简单的
Map<String, Any>
来满足您的用例。
英文:
TL;DR
If you want to access them from outside, those operator fun
shouldn't be extensions of MutableList
.
data class StringList(val str: String) {
val sl = mutableListOf<String>()
val objects = mutableListOf<Any?>()
operator fun set(k: String, value: Any?) {
val t = sl.indexOf(k)
objects[t] = value
}
operator fun get(e: String): Any? {
val t = sl.indexOf(e)
return objects[t]
}
}
Now you can use them like so:
myStringList["keyname"] = "keyvalue"
var tmp = myStringList["keyname"]
Details
There are few things which I'd like to point out here:
-
Extensions functions declared inside a class can be accessed only in that class or in one of its subclasses, so you can still use your version (e.g.
myStringList.objects["keyname"]
) only if you are invoking that operator function insideStringList
. -
I think you should handle the case in which
indexOf()
returns-1
, otherwise the functionsset
andget
will throw an out-of-bounds exception. -
If you can, consider to use a simple
Map<String, Any>
for your use case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论