Jakub Gwóźdź
04/21/2023, 9:31 AM?.
and ?:
but still, I personally find Arrow’s .tap {}
, .map {}
etc more consistent with the rest of the code (eithers, validateds etc)CLOVIS
04/21/2023, 9:37 AMCLOVIS
04/21/2023, 9:40 AMsimon.vergauwen
04/21/2023, 10:21 AMsimon.vergauwen
04/21/2023, 10:22 AMmitch
04/21/2023, 10:25 AMMono.empty
which happens when a null value mistakenly propagated through a reactive channel. Those are hard to debug. However, other teams have also shown that nullables can also be used with extreme caution. Some of the team had experienced unexpected behaviour due to mistakenly propagated / interpreted null when dealing with dynamodb aws sdk 2.0.. That caused some unexplained latency spike.
My team personally chose to using Option, rather consistently. The reason is because it allows us to not think of any possibility of mistakenly propagated / interpreted null. It makes it easier for us to write confidently, without having to worry about integrations between java libs (think opensaml, aws sdk, spring) or nested nullability.. However, it needs to be noted that we also have different teams that went full throttle nulls and they were ok with it.
Another convenience that I found good about option is because I can create various extension functions on them because they are not synthetic types.mitch
04/21/2023, 10:28 AMoption { }
and nullable { }
dsl allows us to write virtually identical program.simon.vergauwen
04/21/2023, 10:28 AMFred Friis
06/26/2023, 7:45 PMJakub Gwóźdź
06/26/2023, 8:06 PMFred Friis
06/26/2023, 8:09 PMCLOVIS
06/26/2023, 9:04 PMnull
is very important for users migrating from Java codebases, and an Option
type is quite expensive on the JVM when used on a large scale (that's why Optional
is not recommended to use anywhere other than function return types). Kotlin's null
handling has been, and remains, the gateway drug for most of the Java community, and I doubt Kotlin would be so popular today if it didn't start like this.CLOVIS
06/26/2023, 9:07 PMorNull
variants).Fred Friis
06/26/2023, 9:23 PMfun getUser(userId: UserId) : Maybe<User>
or
fun getUser(userId: UserId) : Either<Error, User>
CLOVIS
06/26/2023, 9:27 PMbut have an escape hatch compiler argument or somethingThat's a very bad idea. If you do this, you get a fractured language where the rules are different for each codebase. Or you get massive codebases where one day in the past someone relaxed the null safety checks and now we can never enable them again because the entire codebase is incorrect—but hey, it works, right? (that's the state of the TS codebase at my day job)
Fred Friis
06/26/2023, 9:29 PMCLOVIS
06/26/2023, 9:31 PMpossibly by using nulls internally in the compiler, but not exposing it to usersBut that doesn't work if you want to nest them. So now, you either have to always box, or forbid nesting nullable values, which is just
null
with extra syntax.
If you choose the first option, changing the nullability information of a field is a binary-incompatible change for all containers of that type, and a source-incompatible change for Java interop.
If Java didn't autobox/autounbox Integer
automatically, I don't think the Kotlin team would have fused them into a single type in Kotlin, because it would have made the language to complicated to use for interop.CLOVIS
06/26/2023, 9:32 PMFred Friis
06/26/2023, 9:36 PMCLOVIS
06/26/2023, 9:37 PMand the fact that Kotlin devs themselves don't seem to be able to decide how their language should do error handling in general is... well.Yeah, I agree. So far, the idiomatic way is: • exceptions should only be used for things that definitely should not happen, and are not recoverable automatically. • except I/O stuff, which is allowed to use exceptions because explicitly handling all cases is too verbose • domain errors should use sealed class hierarchies to communicate failures. Officially, the recommended solution is to create them yourself. I prefer using Arrow. • absolutely NEVER use Result for error management, and burn all code that does.
Fred Friis
06/26/2023, 9:37 PMCLOVIS
06/26/2023, 9:38 PMmust inherently have slow compilers or whateverActually, that's not the only solution. Languages like Ocaml or Rust handle this very performantly. But they can not interop with a language that doesn't do it exactly the same way as they do. That's not an option for Kotlin, which had to interop with Java.
CLOVIS
06/26/2023, 9:40 PMFred Friis
06/26/2023, 9:40 PMCLOVIS
06/26/2023, 9:41 PMand requires MORE syntax built into the language than just returning an EitherYet, even Arrow is moving away from returning Either, using context receivers instead. At large, devs really hate wrapper types.
CLOVIS
06/26/2023, 9:45 PM!
is a workaround the Kotlin team came up with to avoid forcing developers to use additional syntax to use the value when it's unknown whether it's safe or not. If they went with Maybe
, you would be forced to handle the errors, which would make interop more complicated. It's also single-way; you can't write a Kotlin function that creates such a value.
But again, what is Maybe
? Is it a class? Then, it can't work, because you can't expose nullables to Java anymore. Is it syntax sugar for a nullable reference? Sure, but then you have all the downsides of the nullable values we have, with an added wrapper type.Fred Friis
06/26/2023, 9:49 PMCLOVIS
06/26/2023, 9:50 PMsimon.vergauwen
06/27/2023, 7:22 AMmitch
06/27/2023, 2:25 PMmitch
06/27/2023, 2:38 PMsimon.vergauwen
06/28/2023, 7:17 AMIt looks extremely promising but context receivers is still far away, maybe a year or two if not more..?That's really hard to say. Kotlin 1.9.0 is rolling out, so 2.0 is expected at the end-of-this year. So I am hoping for context receivers next year, but I mean it depends what you want out of them. Completely stable, or are you fine with experimentally multiplatform, or perhaps
@OptIn
🤷 Completely stable is probably some time away, but I doubt most people will wait for that if it's relatively safe to OptIn
. I mean everyone was using "experimental" Kotlin Duration as well.CLOVIS
06/28/2023, 7:53 AMCLOVIS
06/28/2023, 7:55 AMsimon.vergauwen
06/28/2023, 8:07 AMCLOVIS
06/28/2023, 8:43 AMsimon.vergauwen
06/28/2023, 8:44 AMFred Friis
06/28/2023, 1:08 PM