https://github.com/yairm210/purity
Kotlin Compiler Plugin for validating Pure and Readonly functions
https://github.com/yairm210/purity
compiler-plugin kotlin pure
Last synced: about 2 months ago
JSON representation
Kotlin Compiler Plugin for validating Pure and Readonly functions
- Host: GitHub
- URL: https://github.com/yairm210/purity
- Owner: yairm210
- License: apache-2.0
- Created: 2025-05-11T13:34:40.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2025-08-11T17:40:54.000Z (about 2 months ago)
- Last Synced: 2025-08-11T19:36:55.968Z (about 2 months ago)
- Topics: compiler-plugin, kotlin, pure
- Language: Kotlin
- Homepage:
- Size: 929 KB
- Stars: 18
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
#
Purity

[](https://github.com/yairm210/Purity/actions/workflows/gradle.yml)## What is this?
A Kotlin Compiler Plugin for determining and enforcing Pure and Readonly functions.
### Why?
- Communicating and enforcing function intent
- Determining parallelizable calls (Pure functions are parallelizable with anything; Readonly are parallelizable with each other)## Installation + Basic Usage
Install the plugin by adding the following to your `build.gradle.kts`:
```kotlin
plugins {
id("io.github.yairm210.purity-plugin") version "1.2.2"
}dependencies {
implementation("io.github.yairm210:purity-annotations:1.2.2")
}
```Mark pure functions using `@Pure`, and readonly functions using `@Readonly`:
```kotlin
import yairm210.purity.annotations.Pure
import yairm210.purity.annotations.Readonly@Pure
fun pureFunction(x: Int): Int {
return x * 2
}@Readonly
fun readonlyFunction(list: List): Int {
return list.size
}
```### Advanced usage
Further details are available in the [documentation](https://yairm210.github.io/Purity/usage/advanced-usage/)
### Rules
- 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.
### Limitations
- 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## Acknowledgments
Projects that helped me understand how to setup the project:
* [Foso/KotlinCompilerPluginExample](https://github.com/Foso/KotlinCompilerPluginExample)
* [bnorm/kotlin-power-assert](https://github.com/bnorm/kotlin-power-assert)