How do I fix this code?
private val SUB_REGEXES: Map<String, Pair<Regex, String>> =
SUBSTITUTIONS.keys.associate {
it to Pair(Regex("\\b$it\\b", RegexOption.IGNORE_CASE), SUBSTITUTIONS[it])
}
It doesn’t compile because
SUBSTITUTIONS[it]
is of type
String?
instead of the needed `String`; however, I know it is always a
String
because its key is in the map
SUBSTITUTIONS
.
Here’s a rewrite with
associateWith
:
private val SUB_REGEXES: Map<String, Pair<Regex, String>> =
SUBSTITUTIONS.keys.associateWith {
Pair(Regex("\\b$it\\b", RegexOption.IGNORE_CASE), SUBSTITUTIONS[it])
}