English 中文(简体)
Is it possible to compare two Objective-C blocks by content?
原标题:
float pi = 3.14;
float (^piSquare)(void) = ^(void){ return pi * pi; };
float (^piSquare2)(void) = ^(void){ return pi * pi; };

[piSquare isEqualTo: piSquare2]; // -> want it to behave like -isEqualToString...
最佳回答

To expand on Laurent s answer.

A Block is a combination of implementation and data. For two blocks to be equal, they would need to have both the exact same implementation and have captured the exact same data. Comparison, thus, requires comparing both the implementation and the data.

One might think comparing the implementation would be easy. It actually isn t because of the way the compiler s optimizer works.

While comparing simple data is fairly straightforward, blocks can capture objects-- including C++ objects (which might actually work someday)-- and comparison may or may not need to take that into account. A naive implementation would simply do a byte level comparison of the captured contents. However, one might also desire to test equality of objects using the object level comparators.

Then there is the issue of __block variables. A block, itself, doesn t actually have any metadata related to __block captured variables as it doesn t need it to fulfill the requirements of said variables. Thus, comparison couldn t compare __block values without significantly changing compiler codegen.

All of this is to say that, no, it isn t currently possible to compare blocks and to outline some of the reasons why. If you feel that this would be useful, file a bug via http://bugreport.apple.com/ and provide a use case.

问题回答

Putting aside issues of compiler implementation and language design, what you re asking for is provably undecidable (unless you only care about detecting 100% identical programs). Deciding if two programs compute the same function is equivalent to solving the halting problem. This is a classic consequence of Rice s Theorem: Any "interesting" property of Turing machines is undecidable, where "interesting" just means that it s true for some machines and false for others.

Just for fun, here s the proof. Assume we can create a function to decide if two blocks are equivalent, called EQ(b1, b2). Now we ll use that function to solve the halting problem. We create a new function HALT(M, I) that tells us if Turing machine M will halt on input I like so:

BOOL HALT(M,I) {
  return EQ(
    ^(int) {return 0;},
    ^(int) {M(I); return 0;}
  );
}

If M(I) halts then the blocks are equivalent, so HALT(M,I) returns YES. If M(I) doesn t halt then the blocks are not equivalent, so HALT(M,I) returns NO. Note that we don t have to execute the blocks -- our hypothetical EQ function can compute their equivalence just by looking at them.

We have now solved the halting problem, which we know is not possible. Therefore, EQ cannot exist.

I don t think this is possible. Blocks can be roughly seen as advanced functions (with access to global or local variables). The same way you cannot compare functions content, you cannot compare blocks content.

All you can do is to compare their low-level implementation, but I doubt that the compiler will guarantee that two blocks with the same content share their implementation.





相关问题
Find the closest time from a list of times

So, here s the scenario. I have a file with a created time, and I want to choose a time from a list of times that that file s created time is closest or equal too...what would be the best way to ...

PHP Comparison Operators and Data Types

I m currently working through O Reilly s "Programming PHP" and have come across this table titled "Type of comparison performed by the comparison operators": First Operand | Second ...

Loop Until Condition Reached, iPhone

I have a problem here... After a button is pressed I want a loop to run until a condition is reached: - (IBAction)buttonclick1 ... if ((value2ForIf - valueForIf) >= 3) { ... I would like a loop ...

nuSoap or Zend Soap? [closed]

I would like to know the differences between nusoap and ZendSoap, which is the best? what benefits and disadvantages of each? Anyone who has used both technologies could make this comparison? Thank ...

Logic for a file compare

I trying to write a programm for file compare. For example: file1 1 2 3 4 5 file2 1 2 @ 3 4 5 If I do it line by line, I get: 1 == 1; 2 == 2; 3 != @; 4 != 3; 5 != 4; != 5; But, the truth is ...

热门标签