Kotlin无法识别具有字符串或递延值值的地图中的递延/承诺/未来值
val varToValue = mapOf("@id" to arg.userId, "@salary" to arg.salary)
for (variable in varToValue.keys) {
fieldData = fieldData.replace(Regex(variable),if(varToValue[variable] is String) varToValue[variable] else varToValue[variable]?.await() ?: "") //here
}
这里fieldData
是一个字符串,可能包含@id
或@salary
。 arg.id
是类型字符串和arg.salary
的类型deferred< string>
。现在,我调用替换功能的行无法识别等待()
调用。它给了我重命名参考的建议。基本上,我认为它无法识别它是递延类型的。我如何解决这个问题。我可以为递延和普通类型制作不同的地图,但我希望我可以解决一个循环,只有一张地图。
val varToValue = mapOf("@id" to arg.userId, "@salary" to arg.salary)
for (variable in varToValue.keys) {
fieldData = fieldData.replace(Regex(variable),if(varToValue[variable] is String) varToValue[variable] else varToValue[variable]?.await() ?: "") //here
}
Here fieldData
is a string which might contain @id
or @salary
. arg.id
is of type String and arg.salary
is of type Deferred<String>
. Now the line where I call the replace function is not able to identify the await()
call. It is giving me the suggestion of Rename reference. Basically I think it is not able to identify that it is of Deferred type. How do i solve this problem. I can make a different map for Deferred and normal type but I was hoping I could solve in one for loop and just one map.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当它不是
String
时,可以简单地输入vartovalue [variable]
。这似乎为我解决了问题。
Can simply typecast the
varToValue[variable]
when it is not aString
.This seems to solve the problem for me.
问题在于,Kotlin是一种强烈键入的语言,
延期&lt; string&gt;
andstring
是两种不同的类型。解决此问题的最简单方法是将它们都更改为延期
,例如,wraparg.id
plottabledabledebledeferred
:The problem is that Kotlin is a strongly typed language and a
Deferred<String>
andString
are two different types. The easiest way to solve this problem would be to change them both to beDeferred
, for example, wraparg.id
into aCompletableDeferred
: