Discovered what appears to be a bug with the `in` ...
# kotlin-native
n
Discovered what appears to be a bug with the
in
operator. Below is some sample code from a prototype which doesn't work as expected if the
dataType
argument contains one or more space characters (eg " kotlin.String "):
Copy code
fun processDataType(dataType: String): String {
    val kotlinBaseTypes = setOf("<http://kotlin.Int|kotlin.Int>", "kotlin.String", "kotlin.UInt")
    return if (dataType in kotlinBaseTypes) dataType.trim().replace("kotlin.", "")
    else dataType.trim()
}
I'm wondering if the issue is limited to Kotlin Native or if it affects other Kotlin platforms. Replacing
Copy code
return if (dataType in kotlinBaseTypes) dataType.trim().replace("kotlin.", "")
with
Copy code
return if (dataType.trim() in kotlinBaseTypes) dataType.trim().replace("kotlin.", "")
works around the issue.
e
how is that a bug? containers always work that way
r
You seem surprised that
"kotlin.String"
and
" kotlin.String "
are not the same strings, there's no bug here
n
Why does the
in
operator have to match the entire String? Positional matches should be covered by the
in
operator. Provided the text being matched exists in the String, the position shouldn't matter.
e
Copy code
x in container
is equivalent to
Copy code
container.contains(x)
that always means
Copy code
container.any { it == x }
not
Copy code
container.any { it.contains(x) }
👍 1
it couldn't possibly work any other way,
container
could be any type of container, such as
List<Int>
, and
int
and
Int
definitely doesn't have a `in`/`contains`
n
That explains why the
in
operator doesn't behave the same way as the
contains
function.
e
no, the
in
operator behaves the exact same way as the
contains
function, the order of arguments is just reversed
1
image.png
n
Is there a function in the Kotlin std lib that can do text matching, where the position in the String doesn't matter?
e
no
if you want to write
kotlinBaseTypes.any { it in dataType }
then just do that
it makes no sense to do that with a
Set
though