Does Kotlin have a built-in Result type monad, lik...
# getting-started
l
Does Kotlin have a built-in Result type monad, like Rust?
c
l
Hm, interesting. It’s hard coded to use an exception as the failure object?
Are exceptions particularly expensive to make, and then not actually throw? (They are in PHP, but not in Python, which is what I have used pre-Kotlin.)
c
Yes, they are limited to Exceptions, which are expensive to create. The stacktrace is formed when the exception is created
l
Like, I don’t know if it’s bad form to do:
Copy code
if (some error) {
  return Result.failure(CustomException())
}
return Result.success(value)
Hm, so using Result for general validation is probably not wise. Damn. 😞
s
The built in Result type is quite limited, but the folks at #arrow have done a great job with their
Either
type if you want something better
☝️ 1
l
Hm. Can just the Either be used on its own? I don’t think the other project lead on this project would be on board with pulling in the whole library. 🙂
c
This library is a lightweight Result type, which I don’t think restricts to Exceptions https://github.com/michaelbull/kotlin-result
plus1 1
m
arrow is split into many libraries and
Either
is in the core one. You can also look at Kotlin's suggested alternatives. https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/result.md#similar-api-review
☝️ 1
It's also very easy to create your own version of this concept.
c
Personally, I prefer the semantics of
Result
holding a value or an Exception, or
Either
a general union of two types. So I use the built-in
Result
and have a small `Either` class I tend to copy into my projects
e
Are exceptions particularly expensive to make, and then not actually throw?
yes, because they capture stack trace
l
Hm. Yeah, I have a custom sealed class right now, it’s just a bit clumsy. I figured it was worth looking for a more-standard alternative before fleshing it out further.
m
I've not used arrow before, but it seems well designed and popular. Hopefully the core library is small and they have a section on validation. https://arrow-kt.io/learn/typed-errors/validation/
j
If you want only
Either
you can copy it from Arrow. A lot of folks/companies do that
Indeed I think it would be even worth to extract it into its own library an pull it into arrow main lib, cc @simon.vergauwen @raulraja
s
The Arrow Core library is super tiny, in 2.0 which will be released along Kotlin 2.0 it'll contain ~5-7 files? Raise DSL, Either, Ior (Either + Both), NonEmptyList/Set, and some extensions on List. Last time I checked it was approx 400kb JVM Jar, but will probably decrease a bit more before release. The goal of Arrow was to be useful generally to everyone, so we've modularised further and skimmed all the unnecessary stuff. It also needs to be extremely stable, and backwards compatible. This is very important, otherwise it's not useful to use.
l
If only the odds of us being on Kotlin 2 in less than 2 years were good… 😅
s
Kotlin 2 is in RC2 already, I expect it in weeks or months?
So Arrow 2 will land this summer
l
I’m sure it will be out soon. I meant my project using it…
s
Oh okay, sorry 😅 Are you behind for some reason? I've never had any problems upgrading, and rarely hear about it
If there is interest we could do an Arrow 2.0 release on 1.3.x with a Kotlin 1 version if this truly proves to be a problem
l
Eh, I’m the junior lead on this project, new to Kotlin this year, and this 2 year old project I think is, well, 2 years old. I know we’re not on the latest JVM, or Spring, or a few other things.
I don’t have enough karma built up on this team yet to force through “upgrade all the things”. (My last team, that was literally what I spent a year doing.)
t
Falling off the upgrade treadmill really sucks.
163 Views