A Kotlin Compiler Plugin for determining and enforcing Pure and Readonly functions.
- Communicating and enforcing function intent
- Determining parallelizable calls (Pure functions are parallelizable with anything; Readonly are parallelizable with each other)
Install the plugin by adding the following to your build.gradle.kts:
plugins {
id("io.github.yairm210.purity-plugin") version "1.3.2"
}
dependencies {
implementation("io.github.yairm210:purity-annotations:1.3.2")
}Mark pure functions using @Pure, and readonly functions using @Readonly:
import yairm210.purity.annotations.Pure
import yairm210.purity.annotations.Readonly
@Pure
fun pureFunction(x: Int): Int {
return x * 2
}
@Readonly
fun readonlyFunction(list: List<String>): Int {
return list.size
}Further details are available in the documentation
-
Pure functions may not:
- Get or set external vars (vars created outside the function)
- Call other non-pure functions
-
Readonly functions may not:
- Set external vars
- Call other non-readonly functions (pure functions are considered readonly as well)
Any violation of these rules creates a compilation error.
- All getters are assumed to not be state-changing
- All constructors are assumed to be pure - to change state only of the instance being created
Development instructions here
Projects that helped me understand how to setup the project: