# If needs else branch in expression of type Unit

**URL:** <https://discuss.kotlinlang.org/t/if-needs-else-branch-in-expression-of-type-unit/7970>\
**Category:** Language Design\
**Created:** [May 31, 2018, 1:21pm UTC](https://discuss.kotlinlang.org/t/if-needs-else-branch-in-expression-of-type-unit/7970 "2018-05-31T13:21:25Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [May 31, 2018, 7:06pm UTC](https://discuss.kotlinlang.org/t/if-needs-else-branch-in-expression-of-type-unit/7970/5 "2018-05-31T19:06:57Z")

</div>

> [@If operator in function expression](https://discuss.kotlinlang.org/t/if-operator-in-function-expression/7227):
>
> Hello guys, I have bumped into interesting language feature that I cannot make sense of. In Kotlin we can use expression as function body, e.g. fun foo(x: Int, y: Int) = x + y I tried today write following function: fun log() = if (isError()) logError() surprisingly this function couldn’t be compiled. The reason is following: Error:(128, 39) ‘if’ must have both main and ‘else’ branches if used as an expression Can anyone explain me why if listed above is not legit?

---

_[View the full topic](https://discuss.kotlinlang.org/t/if-needs-else-branch-in-expression-of-type-unit/7970)._
