I would like to mark certain functions as pure and have the compiler enforce this guarantee, it would allow it to call only pure functions and disallow calling out to Java. Moreover, the compiler can infer the purity of a function in certain situations and may provide a warning to mark it pure, etc…
example:
pure fun goodPlus(x: Int, y: Int): Int = x + y // ok
pure fun badPlus(x: Int, y: Int): Int {
launchTheMissles() // impure function or call to Java
return (x + y)
} // compile error
// accepts only pure functions
fun higherOrder(f: pure (Int,Int) -> Int): Int = f(100,200)
higherOrder(goodPlus) // ok
higherOrder(badPlus) // compile error
Motivation:
- I want to build a virtual dom(like React) in Kotlin and having a purity guaranty allows a significant performance optimization.
- allows memorization without worries.
- may help the compiler optimize better.
- equational reasoning, easier to comprehend.
- a lot more benefits…
There are many many benefits to referential transparency (purity). I would like the compiler to provide a hard guarantee that can be trusted.