Doesn’t idea of closures go against pure function?With closures, we make function dependent on an external variable,something we don’t want to have for a pure function.
Closing over a
val is pure, closing over a
I and some others tend to use @runarorama’s definition, which is adapted in Functional Programming in Scala, as a useful one (the justification for “useful” is included in that post):
fis pure if the expression
f(x)is referentially transparent for all referentially transparent
And “pure function with no free variables” gets the name combinator.
Can I haz @combinator annotation then?
Seriously, it would be really nice to have a way to stop accidently closing
over state. This is also a performance optimization, as such a function can
be a singleton.
There was a project to provide that, called Spores. I’m not sure how far it
Spores appears abandoned, and was much more ambitious than simply ensuring,
effectively, that the generated lambda constructor is empty.