Reducing the “syntactic castor-oil” in Objective C

| Última modificación: 3 de mayo de 2024 | Tiempo de Lectura: 2 minutos

Algunos de nuestros reconocimientos:

Premios KeepCoding

Reducing the “syntactic castor-oil” in Objective C

I just finished reading “Regarding Objective-C & Copland 2010”, where the author discusses if Objective C can be considered a “modern language”. The provided description for a “modern language” is arguable (and I certainly don’t agree with all the points), but Objective C seems to do pretty well:

Automatic memory management.

Certainly a must, and Objective C does have, at least on the desktop, a GC. Since it’s not available on iOS, let’s say +1/2 for Objective C.

Native Strings

We have @strings, which behave as native strings, even though the repetitive need for @ could be considered as “syntactic castor-oil” or a pain in the semi-colon. Again, +1/2 for Objective C.

Regular expressions, native or via library.

I really don’t agree with this one, but maybe it’s just that regexes and yours trully don’t get along very well. Anyway, we do have them. +1 for Objective C.

Native Object System

Objective C does have it, and pretty good one, BTW. +1 for Objective C.

Named Parameters

Uh? Given Objective C’s syntax, I can’t see the need for such a thing. Objective C offers, a much better solution than named parameters. Named parameters are cool in Python, for example, since they are the poor man’s intersperced parameters for languages with traditional function call syntax. +1 for Objective C.

Succinct Syntax for Common Operations

This is almost embarrassing. Not only Objective C is not succinct (for any kind of operation), it is comically verbose for everything. 0 for Objective C.

Aknowledgement of concurrency

GCD and blocks make a terrific job at making concurrency usable and even palatable, at least for a language not designed from ground up for concurrency (such as Scala). +1 for Objective C.

Single vendor driving the development

Seriously, does this really matter? Anyway, there’s no one besides Apple, at least no one that matters. Sorry GNUStep and Etoilé folks, but you’re not there yet. +1 (although irrelevant) for Objective C.

Six out of eight. Not bad for a language designed back when Microsoft ruled the Earth.

Explicit Arrays and Dictionaries

The most promising improvements would be in the syntax field: reducing the syntactic castor-oil. At this point, it probably wouldn’t make sense to get rid of the @ or at least use it for the real exceptions: since we almost always mean a NSString and not a C string, @”foo” should mean a C string and not an NSString.

There is however, a place where @ could be used to reduce verbosity, by allowing to explicitly describe arrays and dictionaries:

Instead of

[NSArray arrayWithObjects: [NSNumber numberWithInt:3], [NSNumber numberWithInt: 4], nil]

use
@(3,4).

By combining a new syntactic element @() with automagic boxing of native types, we could substantially reduce the verbosity of a very common operation. Same thing could be done with NSDictionary using @{} (for example).

I would definitely like to se something like this in a new version of the language.

Fernando Rodríguez

iOS Developer & Co-Fundador de KeepCoding

Posts más leídos