[] NSArray index and NSDictionary key syntax for other types?

I just discovered the awesome [] operator for NSArray and NSDictionary. A number of places where I use -valueForKey: couldn't translate, because they aren't NSDictionary objects. I did a little reading, and learned that I need to implement -objectForKeyedSubscript:.

I was wondering if it would make sense to add a declarative attribute to indicate which methods should be used for indexed and keyed subscripts of some types. In other words, if I implement -valueForKey: on some class, I'd like to declare to the compiler that that should be used for [key] accesses (similarly, I might implement -setValue:forKey:). If the types are valid, it would use it.

Maybe this really isn't necessary given that I should be able to just implement -objectForKeyedSubscript:, but it would possibly remove an extra message send.

Hi Rick,

I don’t think what you want could be done in a type safe way.

What we have here is essentially an “informal protocol” that all Objective-C objects conform to, but the methods are “optional”. What this means is that you can actually use the subscript syntax on “id”:

  id x = …
  x[key] = …

Since every Objective-C pointer type is convertible to ‘id’, they all need to implement this informal protocol in the same way.

For example, suppose your special class ‘Foo’ had this attribute that said that other methods implement the subscripting. The following would then break:

  Foo *y = …
  y[key] = value; // works
  id x = y;
  x[key] = value; // doesn’t work

The problem here is that for ‘y[key]’ the compiler knows the specific type of the receiver and could use your specially marked methods, but if you assigned the same object to ‘id’ then the compiler would know longer know the correct methods to use and would default to the original ones. This fundamentally breaks type safety for this language feature.

Cheers,
Ted

Hi Rick,

I don’t think what you want could be done in a type safe way.

What we have here is essentially an “informal protocol” that all Objective-C objects conform to, but the methods are “optional”. What this means is that you can actually use the subscript syntax on “id”:

id x = …
x[key] = …

Since every Objective-C pointer type is convertible to ‘id’, they all need to implement this informal protocol in the same way.

For example, suppose your special class ‘Foo’ had this attribute that said that other methods implement the subscripting. The following would then break:

Foo *y = …
y[key] = value; // works
id x = y;
x[key] = value; // doesn’t work

The problem here is that for ‘y[key]’ the compiler knows the specific type of the receiver and could use your specially marked methods, but if you assigned the same object to ‘id’ then the compiler would know longer know the correct methods to use and would default to the original ones. This fundamentally breaks type safety for this language feature.

Well, that's true, but I don't think that's unusual. It could simply issue a warning or error on the use against id, just like the dot-property syntax does.