https://kotlinlang.org logo
Title
w

william

11/10/2019, 3:57 AM
since there are no checked exceptions in kotlin, is it best to have error return values instead?
s

streetsofboston

11/10/2019, 4:17 AM
You still have exceptions that you can catch... they are just not checked. But, if you like function-style programming, making your functions more pure by either carrying a good return value or an error value (e.g.
Either<E,T>
), that'd be better than them throwing an exception...
s

serebit

11/10/2019, 5:38 AM
Imo, exceptions should be exceptional. If your code needs to halt and catch fire when something genuinely unexpected happens, use an exception. If it's expected to happen sometimes, use either a nullable return or a sealed class return if you want more detailed information
💯 2
w

william

11/10/2019, 5:57 AM
makes sense. remind me of programming in c having result codes everywhere
p

Pavlo Liapota

11/10/2019, 11:19 AM
m

Matteo Mirk

11/11/2019, 3:17 PM
Nothing prevents you to catch exception, they’re just not enforced by the compiler, unlike Java. On a functional side, adding to the excellent advice above, you could use
Result
from standard lib (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/index.html) or
Either
if using Arrow (https://arrow-kt.io/docs/arrow/core/either/#either)