7

I want to embrace functional programming, and ensure that my pure functions not change state.

How do I ensure that a function that I intend to be pure only calls other functions which are themselves pure?

I see two ways, which have issues:

  • Commenting - The IDE doesn't easily show comments of functions when calling them
  • Naming - This would make for ugly function names

I'm looking for a way to enforce purity, not just hint at it.

Is there a language feature which would allow a function to be marked as pure, and prevent a marked pure function calling functions unless they themselves are also marked as either pure or pure-but-don't-check-deeper?

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 6
    Afaik this is currently not possible. You might give [#7770](https://github.com/microsoft/TypeScript/issues/7770) an upvote. Also related: https://stackoverflow.com/questions/45834108/can-i-restrict-a-function-to-be-pure-in-typescript – ford04 Nov 29 '20 at 09:48
  • 1
    As someone who spent days trying to track down where my Date object was being mutated, I'll give that issue my upvote, too! – Michael Campbell Dec 03 '20 at 21:20

0 Answers0