Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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...

share|improve this question
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 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. 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 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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
    
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 2 '14 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.