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

Should there be guidelines for preserving global state? #44

Open
yurikhan opened this issue Jul 5, 2017 · 2 comments
Open

Should there be guidelines for preserving global state? #44

yurikhan opened this issue Jul 5, 2017 · 2 comments

Comments

@yurikhan
Copy link

yurikhan commented Jul 5, 2017

Elisp has a lot of mutable global state. Some of it is highly shared. And it is very easy to modify global, highly shared state inadvertently. This includes point, mark, narrowing, match data, current buffer, selected window, selected frame, and who knows what else.

For example, calling (looking-at "[ \t]") to check if we are on whitespace clobbers match data. If this is done in a function, the caller has to wrap its invocation in a (save-match-data) if match data is important.

Proposal:

  • If your function is not specifically intended to modify global shared state, preserve it by wrapping your function body in the appropriate (save-*) or (with-*) macros.
  • If you do modify global shared state, at a minimum, document the fact; ideally, document how you modify it.
  • If you accept function arguments and expect them to modify global shared state, document your expectations. If they are called in a modified shared state context, document that context.
@dgutov
Copy link
Collaborator

dgutov commented Jul 5, 2017

If your function is not specifically intended to modify global shared state, preserve it by wrapping your function body in the appropriate (save-) or (with-) macros.

For the match data, at least, that goes against the current advice on emacs-devel: any function A when calling function B should assume that B can modify the match data. And it should itself wrap the call to B in save-match-data, if it needs it preserved. We could document when a function preserves the match data, however.

As far as moving point or changing buffers go, yes, I think those should be generally documented.

@yurikhan
Copy link
Author

yurikhan commented Jul 5, 2017

That’s actually okay, too.

As an analogy, processor/OS/compiler ABIs document certain registers as “must be preserved across calls” and certain other registers as “may be destroyed by any call”.

We could have a (n incomplete) list of parts of global state which should or need not be preserved.

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

2 participants