David Smith
08/09/2022, 9:23 AMUnit
has to return Unit
? By being able to return anything when a Unit
is expected I have created a bug in my code where some piece of side-effecting code wasn’t “run” to return Unit
but the compiler didn’t pick it up because you can return anything if Unit
is expected. 😞Sam
08/09/2022, 9:26 AMDavid Smith
08/09/2022, 9:34 AMclass Effect<A, B>(val f: (A) -> B)
fun effect(): Effect<Unit, Unit> = Effect { println("launch missiles") }
fun <A, B> runEffect(eff: Effect<A, B>, a: A): B = eff.f(a)
fun wontLaunchMissiles(eff: Effect<Unit, Unit>): Unit = run { effect() }
run { runEffect(effect()) }
because I have returned an Effect<Unit>
not a Unit
Unit
to be returned but for a functional style this return type indicates that you have done some side-effect and so by not returning it explicitly you are losing the ability for the compiler to tell you that you’ve made a mistake. I wondered if anyone knew of some linting rule or experimental compiler flag or something to disable this implicit Unit
return?Sam
08/09/2022, 9:58 AMeffect()
returns a non-unit value so the IDE/compiler should always warn if it’s unusedeffect()
is called without using its result.David Smith
08/09/2022, 10:14 AMSam
08/09/2022, 10:16 AMDavid Smith
08/09/2022, 10:19 AMSam
08/09/2022, 10:26 AMrestrictToAnnotatedMethods
to false if you want. The configuration for that rule looks pretty flexible 👍David Smith
08/09/2022, 10:26 AM