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.
CodePudding user response:
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.
CodePudding user response:
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)