Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Created November 20, 2012 21:14
  • Star 22 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oscarryz/4121184 to your computer and use it in GitHub Desktop.
Brief history of Objective-C square brackets

Brief history of the "square brackets" as I remember it.

One of the most challenging aspects of the language are the square brackets because they look like arrays, as you just mention.

As I remember the language was inspired in smalltalk, but using C to gain speed.

The idea behind smalltalk was to send message as we do with real life objects. For instance if I ask you your age I would say something like this in natural language:

iain what is your age?

And in smalltalk:

iain whatIsYourAge

In smalltalk everything is an object, thus you can send the following message to the number 5.

5 factorial

Well.. to make the parser faster and easier to understand in Objective-C messages where enclosed in square brackets.

[iain whatIsYourAge]; 

Which has the same meaning as functions or methods in "dot" languages like Java, C# and C++

iain.whatIsYourAge();

But I guess they did not think about messages as functions, but something more "natural". Today we are so used to the "dot" and the "()" syntax, that we don't even notice how "unatural" they are anymore..

The methods in Objective-C can also return values, and also receive parameters in their arguments.

For instance assuming we are still working with iain as an object, the following Java:

Broom broom = new Broom();
Date now = new Date();
iain.cleanYourRoomUsing( now, broom ); 

Would be more natural in Objective-C

Broom* aBroom = [Broom broom]; 
Date*  now = [Date date];

[iain cleanYourRoom:now withCleaningTool:broom];

Of course you can also put them in the same line

// "dot" languages java, c++, c#
iain.cleanYourRoom( new Date() , new Broom() );

//Objective-C
[iain cleanYourRoom:[Date date] withCleaningTool:[Broom broom]];

And that's pretty much it. The idea behind Obj-C was to do what smalltalk did, but a lot faster.

The declarations changes of course , but the idea is not too different.

It was something like:

@implementation Man : Person 

- ( void ) cleanYourRoom: (Date *) when withCleaningTool: (Broom *) what
{
   [self yeahSure];
   etc...
}
@end

It would be the "equivalent of "

class Man extends Person {
   public void cleanYourRoom( Date when, Broom what ){
       this.yeahSure();
       etc...
    }
}

I learn Objective-C some 10 yrs ago, using WebObjects on Mac OS X server ( based on NextStep ), so this is not at tutorial at all, there are plenty of resources for that. But instead is a brief history of the "square brackets" and I hope it helps to understand them better.

Please feel free to modify this comment to make it "Objective-C" valid :P

@PrimeTimeTran
Copy link

Thank you for this guide. It was tremendously helpful to me

@asharafshahi
Copy link

👌

@cdcharlebois
Copy link

thank you for this explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment