Comparing two function instances

I would like to be able to compare two instances of a Function to see if they are the same. The obvious question is ‘what do you mean by the same?’. The answer is that reference comparison is too specific, but otherwise tbh I’m not that fussy.

I would like {true;} and {true;} to be recognised as the same thing. I’m not that worried if {true;} and {true} are recognised as the same.

I tried using .basicHash and hash but these produce different values each time:

{true;}.basicHash // different result each time

I would have accepted .asCompileString but any Function with a closure just gives the value { "open Function" }:

var fish = true;
{fish;}.asCompileString // Always gives { "open Function" }

Is what I want possible or do I need to give my design a good hard rethink?

I think that if you compared the varNames, argNames and bytecodes variables in the FunctionDef, that would do it. The FunctionDef is available through theFunction.def.

That is:

  • Are the arguments named the same?
  • Are the variables named the same?
  • Do the two functions do the same operations in the same order?

I can’t think of a way where different functions could answer “yes” to those three questions.

EDIT: You might need to check the constants and selectors arrays too – and selectors can include Functions, so then it becomes recursive – which is straightforward, just mentioning it.

hjh

Thank you very much, I missed the code property.

I think for now myfunc.def.code.hash should meet my needs, but if I find it’s not specific enough I will consider other areas you mentioned.

This seems to do it:

oneFunction.def.compareObject(anotherFunction.def)

hjh