I have created a set using Collections.synchronizedSet<T>(mutableSetOf<T>())
.
SynchronizedSet
has its own implementation of forEach
(synchronized) that differs from the one provided by Iterable.forEach
(not synchronized), however Kotlin's Iterable.forEach
is annotated with @HidesMembers
, so it gets called instead of the synchronized one.
How do I get back the synchronized version of forEach
?
I've come up with my own solution, which seems a bit cleaner than the one proposed by @david-soroko.
fun <T> Iterable<T>.javaForEach(consumer: Consumer<T>) = forEach(consumer)
The main point here is that the Iterable.forEach
takes a Consumer
, Kotlin's forEach
takes a lambda and explicitly passing the Consumer
resolves to the java forEach
.
And just changing forEach
to javaForEach
looks cleaner than always casting.