Wiki Discussion: Using message_hook/3

This is generally not true. Most more recent hooks are defined in the module prolog, but basically any module may define hooks and several libraries do.

I don’t know about numbers. Bottom line is that assert/1 and retract/1 raise an exception when used on a predicate that exists and static. On a non-existing predicate assert/1 creates the predicate as dynamic. Use dynamic/1 if you want to allow dynamic modification later.

Not sure to what this refers. erase/1 and retract/1 behave the same wrt memory management. Neither physically reclaim the clause as it may be involved in other threads or choice points of the calling thread. They mark the clause and reclaiming the memory is left to the (clause) garbage collector. The reason that temporary installed hooks use

    setup_call_cleanup(
        asserta(HookTerm, Ref),
        ProtectedCode,
        erase(Ref))

Is because this makes it trivial remove exactly the added clause. If we use retract/1, it may retract another clause that happens to unify with the one you just added. It may also leave a choice point. Using erase/1 guarantee we have the right clause without any search and without unwanted choice points.

2 Likes