https://kotlinlang.org logo
Title
a

arekolek

05/10/2021, 7:20 PM
1️⃣
fun foo(bar: Bar) {
  if (!bar.canFoo) {
    <http://logger.info|logger.info>("No foo for this bar")
    return
  }
  // Make foo with bar
}
2️⃣
fun foo(bar: Bar) {
  if (!bar.canFoo) {
    return <http://logger.info|logger.info>("No foo for this bar")
  }
  // Make foo with bar
}
2️⃣ 1
1️⃣ 12
l

Luke

05/10/2021, 7:27 PM
I know you really want to save a line, but it would be a line that doesn't make sense to me. The intention is unclear
👆 1
s

Shawn

05/10/2021, 7:59 PM
another thing to consider is that if you find yourself writing
!bar.canFoo
a lot, consider adding (or extending) a complementary
bar.cannotFoo
or whatever the appropriate name would be
a

arekolek

05/10/2021, 8:02 PM
actually this
canFoo
was completely contrived, the question is purely about the
return
It's not that I want to save a line
I was wondering about how Kotlin has
Unit
as a return type to model void functions
and if it makes sense to use
return somethingThatReturnsUnit()
because of that
l

Luke

05/10/2021, 8:06 PM
I have yet to find an example that makes sense, but yes: if it makes sense then please propagate the Unit. I don't think it makes sense here. It might be arguable if you also return a log in the "else" part