chiroptical
05/01/2023, 1:37 PMktor
this weekend reading from a HOCON file. I ended up with this code,
// Truncated for simplicity
fun Application.getDatabaseConfigurationFromEnvironment(): DatabaseConfiguration {
val host = environment.config.propertyOrNull("postgresl.host")?.getString() ?: "localhost"
return DatabaseConfiguration(host = host)
}
Why is this not suspend
? I would have guessed reading from the environment is an effect?
This question is more general than the example. Why are some effects marked suspend and other not? Coming from Haskell and it seems like it would be beneficial to look at this function, see suspend
, and immediately realize it is doing an effect (versus being pure). 🧵chiroptical
05/01/2023, 1:38 PMsuspend
and getting a little confused at the best way to use it. It could be that this is an "interop with Java" thing and therefore suspend is not available? How best to think about pure versus impure in this language?Stephan Schröder
05/01/2023, 2:11 PMsuspend
isn't about being an effect or about being pure , but only about being concurrent.
A few days ago I saw a talk from the KotlinConf23 about where Kotlin is on the spectrum between imparativ and functional (apparantly in the middle being "expression oriented"), unfortunately the all the Kotlinconf-videos have been made invisible again for now 🤷♂️
But there is a compiler plugin that makes Kotlin more functional: Arrow (it adds type classes, whatever that is, and stuff)
On the other hand, you're invited to stick around with the less-than-functional Kotlin (pun intended). I sure do have a good time with it and you might too 🙂chiroptical
05/01/2023, 2:30 PMsuspend
incorrectly. I would really love to look at a function and know if it is pure or not.Stephan Schröder
05/01/2023, 2:56 PMval
being equally long to var
(and is therefore actually being used wherever possible).
Though technically not even val
means that you'll always get the same value. Technically it only means that the property has no setter, but its value can still change:
interface HasX {
val x:Long // seems like it would always return the same value but doesn't have to
}
data class Permanent(
override val x: Long // once initialized will always return the same value
): HasX
class Impermanent: HasX {
override val x: Long get() = System.currentTimeMillis() // will basically never return the same value
}
This is why smartcasting only works when you make a local copy of the value 😉
But like I said, still better than Java where everything is mostly mutable.chiroptical
05/01/2023, 3:16 PMchiroptical
05/01/2023, 3:16 PMStephan Schröder
05/01/2023, 3:54 PMCLOVIS
05/01/2023, 5:37 PMsuspend
).
If there was a Kotlin-first library to deal with files, I would bet on it using suspend
.CLOVIS
05/01/2023, 5:38 PMCLOVIS
05/01/2023, 5:50 PMsuspend
, and lately, context receivers.
Arrow is also not a compiler plugin, it's just a regular library (though they also have compiler plugins and annotation processing for some other projects, like Arrow Optics or Arrow Meta).
I would really love to look at a function and know if it is pure or not.Same, but sadly, adding that is a massive language design decision that essentially leads to Haskell. One of the reasons is that the platforms Kotlin runs on (JVM, JS, Native) don't have a concept of immutability. Another reason is Kotlin's goal to be "the language of the industry", meaning it should be easy to understand and easy to introduce into new codebases. The pure/impure explicit coloring doesn't have enough benefits for its complexity. For a while, the Arrow team recommended to mark impure functions as
suspend
, but I think that has fallen out of use nowadays.
suspend
is implemented with something that looks a lot like the Continuation monad (I'm not an expert there, but there are some limitations), so in that sense, it is the closest thing we have to effects, yes.CLOVIS
05/01/2023, 5:52 PMval
and List
are read-only, not immutable. Of course, it is possible to create truly immutable data structures (as long as you trust your users not to do weird memory accesses). There even is an alpha library for immutable collections: https://github.com/Kotlin/kotlinx.collections.immutableCLOVIS
05/01/2023, 5:53 PMsuspend
and effects: https://arrow-kt.io/learn/design/effects-contexts/CLOVIS
05/01/2023, 5:55 PMchiroptical
05/01/2023, 6:10 PMchiroptical
05/01/2023, 6:10 PMFor a while, the Arrow team recommended to mark impure functions as suspend, but I think that has fallen out of use nowadays.And this is mostly because you have to interact with a lot of existing code, not because it wouldn't be ideal?
chiroptical
05/01/2023, 6:46 PMwith
function to do this in the future?Stephan Schröder
05/01/2023, 7:20 PMmitch
05/01/2023, 10:11 PMmitch
05/01/2023, 10:51 PMeither { }
. I did an interview with developers re context-receivers and context(Raise<E>)
introduced in KotlinConf ’23 and Arrow 2.x, most were able to naturally relate to it and are really excited to use it.
I had the opportunity to note SonarQube scans on various codebases. Though I can’t publish it due to it not being in public domain.. The findings were interesting: Teams using arrow tend to have on average better score in various maintainability metrics, e.g. cognitive complexity, cyclomatic complexity, test coverage, and readability.
It took me a couple of months to finish all the interviews, experiments and documentation.. I’ve documented the interesting takeaway from the experiment in this article if you’re interested to read up. 🙇♂️
https://betterprogramming.pub/typed-error-handling-in-kotlin-11ff25882880chiroptical
05/01/2023, 11:07 PMCLOVIS
05/02/2023, 7:58 AMmitch
05/02/2023, 8:13 AM