Explain why closures are allowed to mutate state in this “simple, well understood” framework.
Whether closures are or are not allowed to mutate state depends on the programming language (or the aim of the programmer, if the programming language doesn't force their way). Closures are a syntactical building block helping functional programming (building functions out of objects is just much more syntactical overhead), but whether a particular closure is a pure function or not is a separate concern. If the function is to be pure (hence satisfying the core of the "framework") then it would not be allowed to mutate state.
Re:The Repeated Deaths of OOP (2015)
by valenterry on Sunday June 06, 2021 @09:19AM
(#27412963)
They are not allowed to. Why do you think they are?
From the Swift docs regarding closures (I would have used Haskell doc reference, but the Haskell docs are so esoteric and vague as to be nearly useless):
“A closure can capture constants and variables from the surrounding context in which it’s defined. The closure can then refer to and modify the values of those constants and variables from within its body, even if the original scope that defined the constants and variables no longer exists.”
https://docs.swift.org/swift-book/LanguageGuide/Closures.htm...
> From the Swift docs regarding closures
What makes you think Swift only supports functional programming?
> I would have used Haskell doc reference
And if you would have, you would have disproved your own point. In Haskell, closures cannot mutate state, because mutable state is not part of the language semantics. It's modeled explicitly via monads.
Re:The Repeated Deaths of OOP (2015)
by valenterry on Sunday June 06, 2021 @10:29AM
(#27413416)
Swift is not a language that enforces code to be pure functional though.
It's a programming paradigm based on simple, well-understood mathematical framework. It's also the starting point for most research in programming language theory.