Newbie question: Can I mark functions as pure?

This might be totally obvious for everybody except me:

Despite Scala not being a pure language, can I mark specific functions as pure, so that the compiler tells me if I do state changes or IO in these functions?

(If I recall it correctly, Nim can do this.)

2 Likes

Not yet. This sort of thing is a topic of active research (last I checked, anyway), and it might show up in the mainline language eventually, but it’s fairly challenging to add in a way that is both ergonomic and sufficiently powerful to be useful. (And general in a Scala-esque way.) Hopefully someday…

2 Likes

In scala 3 there is an experimental pureFuns import you can try: Pure Function Syntax, note that it is only a syntax sugar, rather than any extra checks/guarantees

4 Likes

Thanks for the link. I hope one day it will include extra checks! :smile:

I think this would be a very valuable addition to the language, once fully implemented. And, although it is still in the experimental corner, can we at least be sure the syntax will stable? Otherwise it does not make much sense using this upfront. (I’m not a fan of references to language.experimental in production code)