make "code" work on more types of hooks
This should work:
i1 : addHook(ZZ,foo,cosh)
o1 = cosh
o1 : CompiledFunction
i2 : addHook(ZZ,foo,x->x^3)
o2 = -*Function[stdio:2:17-2:21]*-
o2 : FunctionClosure
i3 : hooks (ZZ,foo)
o3 = {0 => (foo, Strategy => 0)}
{1 => (foo, Strategy => 1)}
o3 : NumberedVerticalList
i4 : code oo
o4 = -- no method function found: foo()
---------------------------------
-- no method function found: foo()
Originally posted by @DanGrayson in https://github.com/Macaulay2/M2/pull/1385#discussion_r531605063
I don't think it should work, here is why, quoted from that thread:
Notice that in
hooks(ZZ, foo), the entries "(foo, Strategy => i)" don't include any reference to ZZ. This is because you intentionally wanted the hook to be stored inZZ#foorather thanZZ.Hooks#foo. If you insist on doing this, you can access the function usingZZ#foo. Otherwise, you should use the other way of installing a hook:
i1 : addHook((foo, ZZ),cosh)
o1 = cosh
o1 : CompiledFunction
i2 : addHook((foo, ZZ),x->x^3)
o2 = -*Function[stdio:2:20-2:24]*-
o2 : FunctionClosure
i3 : hooks (foo, ZZ)
o3 = {0 => (foo, ZZ, Strategy => 0)}
{1 => (foo, ZZ, Strategy => 1)}
o3 : NumberedVerticalList
i4 : code oo
o4 = -- code for method: foo(ZZ)
function cosh: source code not available
---------------------------------
-- code for method: foo(ZZ)
This is a good reason to deprecate the addHook(MutableHashTable, Thing, Function) syntax for good, since it's an anti-pattern.
However, for now, this syntax is almost never used anywhere, so I don't see an urgency for either deprecating it or making hooks work the way you imagine.
It should work. A good way for it to work would be for "hooks" a list of pairs, where the first entry is the storage location (a mutable hash table), and the second entry is the key under which the hooks are stored.
This is a good reason to deprecate the
addHook(MutableHashTable, Thing, Function)syntax for good, since it's an anti-pattern.
That depends on what you think the pattern is. I think that is the pattern.
A related improvement is to make hooks(intersect, Ideal) list (intersect, Ideal, Ideal, Strategy => ...). Currently it doesn't, because there is no method (intersect, Ideal, Ideal), as it gets included with (intersect, List) or (intersect, Sequence). Should we add that method, even though it is never used, or change the hook key to (intersect, Ideal), or change hooks to also list that hook? I'm not sure.