https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
n

napperley

10/06/2023, 1:22 AM
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

ephemient

10/06/2023, 2:18 AM
how is that a bug? containers always work that way
r

ribesg

10/06/2023, 10:37 AM
You seem surprised that
"kotlin.String"
and
" kotlin.String "
are not the same strings, there's no bug here
n

napperley

10/09/2023, 8:32 PM
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

ephemient

10/09/2023, 8:33 PM
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

napperley

10/09/2023, 8:35 PM
That explains why the
in
operator doesn't behave the same way as the
contains
function.
e

ephemient

10/09/2023, 8:36 PM
no, the
in
operator behaves the exact same way as the
contains
function, the order of arguments is just reversed
1
image.png
n

napperley

10/09/2023, 8:39 PM
Is there a function in the Kotlin std lib that can do text matching, where the position in the String doesn't matter?
e

ephemient

10/09/2023, 8:39 PM
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