Arc Forumnew | comments | leaders | submitlogin
2 points by shader 2300 days ago | link | parent

The idea behind a 'hook' is that it is a place where code can be inserted without having to modify the original code. They're pretty common in emacs, for instance, where lots of packages provide hooks for easy extension in your .emacs file.

Arc hooks are apparently pretty lightweight:

  (= hooks* (table))

  (def hook (name . args)
    (aif (hooks* name) (apply it args)))

  (mac defhook (name . rest)
    `(= (hooks* ',name) (fn ,@rest)))
(from https://github.com/arclanguage/anarki/blob/7a1fba03b6faaa06f...)

So, if there's a function bound to that name in the hooks table, it is run with the arguments. You only call 'defhook when you want to bind to that point in the code.

This is a rather interesting implementation to me, because I'm used to the emacs-lisp concept, where a hook is actually a list of functions that all get called on the arguments, so it can be added to multiple times. The corresponding names in elisp are 'run-hooks and 'add-hook. https://www.gnu.org/software/emacs/manual/html_node/elisp/Ho...



2 points by hjek 2300 days ago | link

Ok, makes sense. Thanks for the explanation!

So I guess it's on purpose that `(hook 'somewhere)` doesn't do anything until you define a hook for that place yourself, e.g.:

    (defhook somewhere () (pr "Hello from somewhere"))

-----