0

Languages like Haskell have the concept of pure functions and will not compile if a function that is supposed to be pure is in fact not pure.

e.g. if I have

processFoo :: (() -> Foo) -> Boolean

and pass in

getFooFromServer :: () -> IO Foo

as the first argument, then the code does not compile and I get an error. This is useful because I easily get to see which parts of my code are pure (and so easily testable, don't do any mutation, etc etc), and which parts form the boundary to external systems.

As far as I know there are no languages that offer something similar where a function can be marked as pure and if it

  1. does an IO operation
  2. relies on or mutates global state, or
  3. calls a function that is not also pure

then the compiler/interpreter throws an error, e.g.

Foo getFooFromServer () {
  // ... do some IO
}

void pure processFoo() {
  getFooFromServer()
  // ^ compile error - not a pure function
}

Question: Is there such a language?

Jonny
  • 2,509
  • 23
  • 42

0 Answers0