1

The question says it all. Why:

(void)methodWithParamA:(id)paramA paramB:(id)paramB;
[obj methodWithParamA:valA paramB:valB];

and:

void(^ block)(id paramA, id paramB);
block(valA, valB);

I'm not sure I'll necessarily gain anything by learning the answer to this question, but I'm baffled that one language can have so many disparate syntaxes...

ericsoco
  • 24,913
  • 29
  • 97
  • 127
  • 4
    The latter is (almost) plain C, as is a lot of the non-object related stuff in Objective C. The square bracket stuff is Objective C's extension to the language for handling objects. The language is quite different from C in that regard, but maintains a lot of C-like style (mostly I believe for compatibility with C library headers). – Dave Mar 29 '14 at 17:21
  • 1
    [Here is an excellent post](http://nilsou.com/blog/2013/08/21/objective-c-blocks-syntax/) on the subject that may help you – Alladinian Mar 29 '14 at 17:28
  • @Dave - The later is plain C. It is not almost plain C. https://en.wikipedia.org/wiki/Blocks_(C_language_extension) – ArtOfWarfare Jul 24 '14 at 21:07
  • @ArtOfWarfare a non-standard extension is not plain C. – Dave Jul 25 '14 at 11:18

1 Answers1

3

A block is a function, there is no "current instance" (or "current class" for call methods) as with a method.

A block call therefore looks like a function call; a "block" type is a pointer type and follows very closely the syntax for function pointer types; and a block body follows closely the syntax of a function body.

Blocks are supported in C, which has functions but not methods.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Reading this makes me ask the counterpart question: why the square-bracket-space-delimited-params syntax of Objective-C methods? I'm learning about blocks now, and thinking it's strange that they're syntactically so different from methods. Reading your explanation that blocks are modeled on C functions, now I'm back to thinking it's strange that Objective-C methods look like they do... – ericsoco Mar 29 '14 at 18:28
  • 1
    I guess @Dave answered this question in his comment above -- Objective-C methods came after C functions, and the language creators decided they needed a completely different syntax. Sigh. – ericsoco Mar 29 '14 at 18:29
  • 1
    @ericsoco - In Objective-C you *call* a function, while you *send a message* to an object to invoke a method. The two processes are quite different. The former is direct and static - it is determined where you end up at compile time. The latter is indirect and dynamic - at runtime a search is performed to find a method to match the message, and this may fail. Hence having different syntaxes makes a lot of sense, and the syntax follows that of Smalltalk. In C++ a method call is not as distinct from a function call and they use a similar syntax. – CRD Mar 29 '14 at 18:37
  • @ericsoco: The keyword-colon-argument phrases separated by spaces part of the syntax is from Smalltalk. The square brackets I think is just to make the new syntax not conflict with C syntax. – newacct Apr 02 '14 at 09:31