Kotlin无法识别具有字符串或递延值值的地图中的递延/承诺/未来值

发布于 2025-02-08 13:35:50 字数 627 浏览 1 评论 0原文

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@salaryarg.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以为你会在 2025-02-15 13:35:50

当它不是String时,可以简单地输入vartovalue [variable]

fieldData = fieldData.replace(Regex(variable),if(varToValue[variable] is String)  varToValue[variable] else (varToValue[variable] as Deferred<String>)?.await())

这似乎为我解决了问题。

Can simply typecast the varToValue[variable] when it is not a String.

fieldData = fieldData.replace(Regex(variable),if(varToValue[variable] is String)  varToValue[variable] else (varToValue[variable] as Deferred<String>)?.await())

This seems to solve the problem for me.

绝對不後悔。 2025-02-15 13:35:50

问题在于,Kotlin是一种强烈键入的语言,延期&lt; string&gt; and string是两种不同的类型。解决此问题的最简单方法是将它们都更改为延期,例如,wrap arg.id plottabledabledebledeferred

val varToValue = mapOf("@id" to CompletableDeferred(arg.userId), "@salary" to arg.salary)

The problem is that Kotlin is a strongly typed language and a Deferred<String> and String are two different types. The easiest way to solve this problem would be to change them both to be Deferred, for example, wrap arg.id into a CompletableDeferred:

val varToValue = mapOf("@id" to CompletableDeferred(arg.userId), "@salary" to arg.salary)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文