Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better explain how pure is different from side effect free #3

Open
calvinmetcalf opened this issue Jul 5, 2023 · 2 comments
Open

Comments

@calvinmetcalf
Copy link

It took me a couple readings to realize pure no use of closures. Making it more explicit what the two things do would by helpful. My understanding:

  • side effect free: does not write to any variable outside the function and does not modify it's arguments if they are objects
  • pure: same as above but also does not read from any external variables besides it's arguments
@antfu
Copy link
Member

antfu commented Jul 6, 2023

I agree. But I feel that the problem is that __PURE__ is used on expressions, but not on function declarations, which does not apply to the statement you mentioned?

From my understanding:

  • __PURE__ marks expressions as "side-effect-free"
  • __NO_SIDE_EFFECTS__ marks functions as "side-effect-free"

So there is actually no "pure function" concept involved.

@robpalme
Copy link

robpalme commented Jul 6, 2023

The semantic relationship I have always understood is:

  • side effect: Calling this function may mutate state outside the function, e.g. mutation of non-primitive arguments
  • side cause: This function's return value may depend on state outside the function, e.g. a closure-captured variable
  • pure: This function has no side effects and no side causes.

I agree this relation should be clarified somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants