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

Is it possible to compose hook handlers? #2

Open
Ambrevar opened this issue Aug 30, 2019 · 9 comments
Open

Is it possible to compose hook handlers? #2

Ambrevar opened this issue Aug 30, 2019 · 9 comments

Comments

@Ambrevar
Copy link

Say I have a hook of handlers with 1 argument returning 1 argument.
If would like to run the successive handlers of the result of the previous one.
Looking at the code of run-hook it does not seems possible directly.

I came up with the following function:

(defun run-composed-hook (hook &rest args)
  (apply (apply #'alexandria:compose hook) args))

It works, but my main concern is that this is not part of the description of the hook, the behaviour of indeed decided at the call site, not by the hook itself.

The only way to fix it, as I understand it, is to implement this "composability" feature in cl-hooks itself. Thoughts?

@Ambrevar
Copy link
Author

To implement this, I suggest a "handler-combination" option. Default would be #'mapcar for instance, my above suggestion would be #'alexandria:compose, but we could imagine others, like #'or (run hooks until one returns non-nil) and #'and (run hooks until one returns nil).

@scymtym
Copy link
Owner

scymtym commented Aug 31, 2019

You are right that execution order and composition of hooks is not customizable at the moment. The current combination implementation only applies to results after all hooks have been run independently. It is like method combination without call-next-method. Having hooks execute independently was a conscious design decision at the time.

That said, I don't see why additional customization shouldn't be possible. I will have to think about how to generalize the current mechanism. Your suggestion sounds like one possibility, but the interaction with the current combination option is unclear since with compose execution there are no multiple results to combine.

@Ambrevar
Copy link
Author

Ambrevar commented Aug 31, 2019 via email

@scymtym
Copy link
Owner

scymtym commented Sep 7, 2019

Sorry for taking a long time here. I haven't forgotten about this. Despite not having a lot of time to think about this, I can say right away that I don't like the warning idea too much. I hope to combine composition and combination into a single mechanism so that the problem doesn't arise.

@Ambrevar
Copy link
Author

Ambrevar commented Sep 7, 2019 via email

@scymtym
Copy link
Owner

scymtym commented Sep 13, 2019

Sorry for not getting back to you earlier. I can't making any promises about accepting changes. I suggest you fork the repository and work in your fork for now.

@Ambrevar
Copy link
Author

Intuitiveness question: cl-hooks are first-in-last-out queues.
Which means if 2 handlers modify the same variable, the last one added will be overridden by the first. I think the most expected behaviour is the reverse. Thoughts?

Within the context of the current issue, it would be as simple as using #'reverse as composition function. @vindarel?

@scymtym
Copy link
Owner

scymtym commented Sep 26, 2019

@Ambrevar Btw, are you aware of https://gitlab.com/Gnuxie/method-hooks? From a brief look, it seems to have some of the features you are proposing.

@Ambrevar
Copy link
Author

Ambrevar commented Sep 26, 2019 via email

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