Why future has side effects?

前端 未结 4 458
太阳男子
太阳男子 2020-12-16 18:13

I am reading the book FPiS and on the page 107 the author says:

We should note that Future doesn’t have a purely functional interface. This is part

4条回答
  •  臣服心动
    2020-12-16 18:26

    A basic premise of FP is referential transparency. In other words, avoiding side effects.

    What's a side effect? From Wikipedia:

    In computer science, a function or expression is said to have a side effect if it modifies some state outside its scope or has an observable interaction with its calling functions or the outside world. (Except, by convention, returning a value: returning a value has an effect on the calling function, but this is usually not considered as a side effect.)

    And what is a Scala future? From the documentation page:

    A Future is a placeholder object for a value that may not yet exist.

    So a future can transition from a not-yet-existing-value to an existing-value without any interaction from or with the rest of the program, and, as you quoted: "methods on Future rely on side effects."

    It would appear that Scala futures do not maintain referential transparency.

提交回复
热议问题