Am I abusing unsafePerformIO?

前端 未结 4 1074
天命终不由人
天命终不由人 2020-12-28 14:25

To get acquainted with unsafePerformIO (how to use it and when to use it), I\'ve implemented a module for generating unique values.

Here\'s what I have:

4条回答
  •  我在风中等你
    2020-12-28 14:53

    The purpose of unsafePerformIO is when your function does some action internally, but has no side effects that an observer would notice. For example, a function that take a vector, copies it, quicksorts the copy in-place, then returns the copy. (see comments) Each of these operations has side effects, and so is in IO, but the overall result does not.

    newUnique must be an IO action because it generates something different every time. This is basically the definition of IO, it means a verb, as opposed to functions which are adjectives. A function will always return the same result for the same arguments. This is called referential transparency.

    For valid uses of unsafePerformIO, see this question.

提交回复
热议问题