https://kotlinlang.org logo
#getting-started
Title
# getting-started
b

Ben Edwards

08/23/2022, 3:54 PM
item in array or array.contains(item). What made me laugh was https://stackoverflow.com/questions/42234775/kotlin-idiomatic-way-to-check-array-contains-value. The posters seemed to want to find the cleverest, most complex way of doing it rather than answering the question. 'item in array' seems the most elegant but is it idiomatic?
j

Joffrey

08/23/2022, 3:59 PM
I think the accepted answer is pretty reasonable, I don't quite get why this doesn't answer your question. Basically you can use both depending on how readable you find them. I usually use
in
inside
if
conditions because I like how it reads. Sometimes
contains
might feel more natural if it's at the end of a chain of calls. In other cases, I don't mind, I guess it's opinion based anyway
By the way, you might want to ask those in #codingconventions
👃 1
e

ephemient

08/23/2022, 3:59 PM
item in array
is idiomatic
the use of
Array
in general is not
b

Ben Edwards

08/23/2022, 4:13 PM
Thanks
@Joffrey another great channel.
Think I may have found a case for ArrayList
fun dropLoot(name: String): Boolean {
println("$name will be dropped")
return inventory.removeIf { it.name = name }
}
cant work out how to do this for mutanleListOf
e

ephemient

08/23/2022, 4:39 PM
the Kotlin method is
Copy code
inventory.removeAll { it.name == name }
removeIf
comes from Java
a

August Lilleaas

08/23/2022, 4:41 PM
removeIf
seems to work on
mutableListOf
🤔
but it won’t work like written in
dropLoot
since it mutates the list, and returns a boolean
b

Ben Edwards

08/23/2022, 4:46 PM
Sorry don't quite follow
a

August Lilleaas

08/23/2022, 4:48 PM
I suppose you want
==
there and not
=
1
e

ephemient

08/23/2022, 4:49 PM
you must have set something up wrong;
removeIf
is one of the new default methods in Java 8: https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate- and as such it is callable on a
MutableList
as long as you're on JVM
==
vs
=
would break regardless of `ArrayList`/`MutableList`…
b

Ben Edwards

08/23/2022, 4:51 PM
OK, but removeIf still red. Think I may need to use removeAll, just looking into syntax
a

August Lilleaas

08/23/2022, 4:53 PM
seems to work on my machine at least 🙂
Copy code
data class Thing(
        val name: String
    )

    val inventory = mutableListOf<Thing>(Thing("foo"), Thing("bar"), Thing("baz"))
    println(inventory) // [Thing(name=foo), Thing(name=bar), Thing(name=baz)]
    inventory.removeIf { it.name == "foo" }
    println(inventory) // [Thing(name=bar), Thing(name=baz)]
b

Ben Edwards

08/23/2022, 5:00 PM
Interesting
But I can do it with
Copy code
fun dropLoot(name: String): Boolean {
    println("$name will be dropped")
    return inventory.retainAll { it.name != name }
}
e

ephemient

08/23/2022, 5:08 PM
that may mean you don't have jvmTarget="1.8"
anyhow, largely irrelevant; just use Kotlin's
removeAll
or
retainAll
.
b

Ben Edwards

08/23/2022, 5:13 PM
Thanks, where do I find my jvmTarget? I'm using the latest Android Studio.
j

Joffrey

08/23/2022, 7:48 PM
@Ben Edwards it should be in one of your
build.gradle(.kts)
files - or nowhere at all, but I think the default is now 1.8 so if you have no such setting you should also have no such problem 😄
👃 1
4 Views