Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Protocol method is marked @required, when not implemented, why does compiler issue a warning and not an error?

Assume that:

  • New Protocol is declared
  • Method in this protocol is marked @required
  • Class conforms to Protocol
  • Class does not implement the method mentioned in Protocol

At compile time, information about this method is known: i.e. that it is required and that this class and any other classes this class may may extend do not implement it.

Why in this case the compiler issues a warning and not an error?

like image 950
James Raitsev Avatar asked Dec 18 '11 01:12

James Raitsev


2 Answers

Errors are only issued when the compiler cannot continue because something went terribly wrong.

When calling a method in Objective-C, the method lookup is done during runtime and not during compilation, which C++ does. In Objective-C a "message" is simply sent to the object, something like obj.executeCommand("Hey, can you execute function <name> for me?"). In C++ the object will be called directly, in a way like obj.<name>(). In the case of Objective-C the executeCommand() method is called, which exists. In C++'s case the function is called but it does not exist. These are methods that are linked on the compiler level, which means they both become memory addresses rather than names. executeCommand becomes 0x12345678 but it still uses the same message ("execute function <name>").

This is probably very confusing, but it's related to the way methods are implemented in different languages.

like image 51
Tom van der Woerdt Avatar answered Oct 19 '22 06:10

Tom van der Woerdt


If you feel strongly about it, why not turn on -Werror?

like image 42
matt Avatar answered Oct 19 '22 05:10

matt