Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Objective-C use square brackets for messages?

I'm reading a book about Smalltalk (Charmond Liu's Smalltalk, Objects and Design). He talks about how messages work in Smalltalk using a HomeBudget object as an example of how to send a single message with multiple parameters:

HomeBudget spend: 229 on: 'VCR'

Objective-C uses a lot of the ideas from Smalltalk, such as messaging objects and colons for parameters, but always encloses a message send in square brackets. The same call would be:

[homeBudget spend:229 on:@"VCR"];

Why does Objective-C use square brackets (instead of leaving messages bare like Smalltalk)? Is it required syntax because of the fact that it's C-based? Is it a stylistic choice? Does it offer more choice in what you can express? Is there another reason?

like image 939
nevan king Avatar asked May 18 '14 15:05

nevan king


People also ask

What do square brackets mean in Objective-C?

In most cases, you don't add the "get" prefix to getters in Objective-C. Whenever you see code inside square brackets, you are sending a message to an object or a class.

Why square brackets are used in C?

Square brackets are used to index (access) elements in arrays and also Strings.

What is the point of square brackets in writing?

Use square brackets to include words within a quote that are not part of the original quote. For example, if a quoted passage is not entirely clear, words enclosed in square brackets can be added to clarify the meaning.

What is Objective-C in iOS?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.


2 Answers

I found an interview with Brad Cox that seems to hint that square brackets were for syntax:

DD: The square brackets are a distinctive part of Objective-C syntax. How did the square brackets come about and was this your doing?

BC: Yeah, that was me. Quite literally it was a search for something that wasn't taken. Curly braces were taken. Ordinary parentheses were taken. It was just a search for braces that wouldn't collide with something C used.

Tom Love also credits himself with the square brackets in Masterminds of Programming, but he seem to be saying that it was a stylistic choice:

I often refer to myself as the guy responsible for the square brackets in Objective-C, because Brad and I had a long conversation about. Do we have a C syntax that is consistently C, or do we create a hybrid language where I describe it as “the square bracket is a gear shift into the object land”? Our view was that if you had a hybrid language, you could build a set of foundation classes so that at some point most of the work is actually done inside the square brackets. This allows a lot of details to be hidden from a typical application programmer.

The square brackets are an indication of a message sent in Objective-C. The original idea was that once you built up a set of libraries of classes, then you’re going to spend most of your time actually operating inside the square brackets, so you’re really doing object-oriented programming using an underlying framework of objects that were developed in the hybrid language, which was a combination of procedural and object-oriented language. Then as you began to build up libraries of functionality, there’s less and less requirement to drop into the procedural world and you could stay within the square brackets. It was a deliberate decision to design a language that essentially had two levels— once you had built up enough capability, you could operate at the higher level. I actually think that’s one of the reasons. Had we chosen a very C-like syntax, I’m not sure anybody would know the name of the language anymore and it wouldn’t likely still be in use anywhere.

like image 54
nevan king Avatar answered Oct 05 '22 23:10

nevan king


Objective-C is a strict superset of "C" and as such all additions were required not to alter the "C" language. That all "C" programs were also valid Objective-C programs.

C++ OTHO did make changes to the "C" language such that not all valid "C" programs will compile with a C++ compiler, are not also valid C++ programs.

So it was necessary to find symbols that were not used in "C" or at least not used in a conflicting context. Thus a lot of usage of "@" in Objective-C. In other cases to add symbols in order to disambiguate from "C".

like image 29
zaph Avatar answered Oct 05 '22 23:10

zaph