https://kotlinlang.org logo
#announcements
Title
# announcements
w

wathek

08/07/2019, 5:07 PM
Hi wonderful people, I'm wondering what's the difference between those two lines of code:
Copy code
// First method
val test = Mono.just("hello").run { block() }

// Second method
val test = Mono.just("hello").block()
Both cases
test
has
String?
type
k

karelpeeters

08/07/2019, 5:10 PM
They're pretty much the same The only technical difference I can think of is that
block
in the first one doesn't have to be defined on the subject, it can be a top level function or something from the surrounding scope.
👍 1
w

wathek

08/07/2019, 5:27 PM
@karelpeeters well actually IntelliJ says
inappropriate blocking method call
and it suggests to write it either using
run
or
with
k

karelpeeters

08/07/2019, 5:27 PM
when
? Are you sure? And is this in a coroutine context or something?
w

wathek

08/07/2019, 5:28 PM
sorry I mean
with
@karelpeeters it's inside a method that returns a
Mono
k

karelpeeters

08/07/2019, 5:29 PM
Can you show a screenshot of some surrounding code including the warning?
w

wathek

08/07/2019, 5:33 PM
@karelpeeters yeah sure here it is
k

karelpeeters

08/07/2019, 5:34 PM
Huh, where does
block
come from then?
w

wathek

08/07/2019, 5:36 PM
it's from
Mono
according to the documentation
Copy code
* Subscribe to this {@link Mono} and <strong>block indefinitely</strong> until a next signal is
	 * received. Returns that value, or null if the Mono completes empty. In case the Mono
	 * errors, the original exception is thrown (wrapped in a {@link RuntimeException} if
	 * it was a checked exception).
k

karelpeeters

08/07/2019, 5:40 PM
Hmm that's really strange, I have no idea why
run
or
with
would help unless Mono has specific versions as well.
👍 1
w

wathek

08/07/2019, 5:41 PM
thanks @karelpeeters
w

wasyl

08/08/2019, 7:46 AM
Isn’t this just IntelliJ inspection? I don’t think it has anything to do with compiler or anything really. It seems to me like writing
.run { block() }
isn’t detected by that particular inspection
You could go to settings->inspections, disable
inappropriate thread-blocking method call
and see if the warning goes away. The description says
Reports thread-blocking method calls found in a code fragment where a thread should not be blocked  (e.g. Reactive frameworks, Kotlin coroutines)
so there’s good chance it’s just IntelliJ being helpful and saying you shouldn’t be using blocking methods in reactive frameworks
k

karelpeeters

08/08/2019, 8:31 AM
But then why would using
run
or
with
help at all? They just shuffle parameter names around, they don't change anything about the blocking.
w

wasyl

08/08/2019, 8:53 AM
@karelpeeters I’d say the way inspection is implemented, it doesn’t detect such usage — it only recognizes e.g. calling
block
on
Mono
type, and not within
Mono
scope. I’d guess
.let { it.block() }
would also show a warning
k

karelpeeters

08/08/2019, 3:47 PM
But why would it suggests such a brittle way to supress the inspection instead of just
@Supress
?
🤔 1
w

wasyl

08/08/2019, 6:01 PM
I can’t seem to reproduce this warning in either AndroidStudio or IntelliJ 2019.2