https://kotlinlang.org logo
j

Joe

08/17/2020, 5:30 PM
(slightly prematurely) looking at 1.4, and seeing a new compiler warning I don't quite understand. In the following class: https://github.com/trib3/leakycauldron/blob/master/testing/src/test/kotlin/com/trib3/testing/db/DAOTestBaseTest.kt lines 30 and 48 both look like:
Copy code
var reached = false
With 1.4, line 30 triggers the following warning, but line 48 does not:
Copy code
DAOTestBaseTest.kt: (30, 23) Variable 'reached' initializer is redundant
I can fix by replacing with
var reached: Boolean
, but would like to understand why the 2 instances are being treated differently first.
n

Nir

08/17/2020, 5:32 PM
Aren't asserts run conditionally?
I guess it depends what flavor of assert this is
Anyhow, I don't know if the compiler is wrong to think that, but clearly the difference is that the subsequent write is wrapped in an
assertThat
j

Joe

08/17/2020, 5:34 PM
asserts from from assertk, tests run via testng, fwiw
n

Nir

08/17/2020, 5:34 PM
The compiler feels confident that the code inside
use
will simply always run
So it calls out line 30 as a dead write
To say that line 48 is a dead write, you need to not that both
use
and
assertThat
unconditionally call the passed lambda
*know that
so I would conclude from this that the compiler for whatever reason isn't comfortable assuming that
assertThat
always executes its lambda.
j

Joe

08/17/2020, 5:38 PM
ah,
dead write
makes sense as an explanation to me, clearer than redundant initializer at least, thanks!
n

Nir

08/17/2020, 5:40 PM
np
4 Views