Re: Future Objective-C changes
Re: Future Objective-C changes
- Subject: Re: Future Objective-C changes
- From: Sheehan Olver <email@hidden>
- Date: Sun, 18 May 2003 00:27:38 -0500
I'd thought I'd point out the reasons java needs generics (well, not
needs, but should have), which in this case doesn't apply so much to
Objective-C. The main use is in the collection classes such as
ArrayList, HashMap, etc. (equivalent to NSMutableArray and
NSMutableDictionary). The issue is suppose you have an ArrayList thats
supposed to consist only of Strings. Generics help in two ways.
First, they prevent you from adding an object thats not a string, for
example the following code with fail at compile time:
ArrayList stringList = new ArrayList<String>(); // I have no idea
what the real syntax is
stringList.add(new File("foo.txt"));
When working in a multi-programmer environment, typing an ArrayList
can often help clarify the purpose it serves, and prevent other people
from accidently using the list in the wrong way. The other thing it
does, which definitely has no use in Objective-C, is lets you remove
excessive casting. In java 1.4, in order to access a string you have
to do something like:
String foo = (String) untypedList.get(0);
String fooSublist = ((String) untypedList.get(0)).substring(0, 5);
As you can see, this is pretty ugly. With generics, we can do:
String foo = stringList.get(0);
String fooSublist = stringList.get(0).substring(0, 5);
Much cleaner, and in the second case, much much easier to read. Of
course Objective-C has almost no use for generics, since there isn't
the same need for constant casting. Though what might be useful would
be to specify a type for an NSMutableArray, in order to get a runtime
exception when something gets added by mistake. Of course, this doesn't
require language constructs, simply a new constructor like:
[NSMutableArray arrayOfType:[NSString class]];
Of the original items, there is only one thing that I feel Objective-C
would benefit from, that is a java-like package system. 2 character
prefixes to classes is non-intuitive, non-descriptive, doesn't scale,
and is ugly. A package name like com.apple.foundation.String at the top
of a file has more use than having NS before String, as it tells you
both the company and the framework the class came from. NS is
completely meaningless, especially since there is no NS in the name
Apple :) Plus, there are only a limited amount of combinations. If
cocoa ever becomes widely popular, there are going to be a lot of
conflicts. We already have support for java objects in Objective-C. How
cool would it be if you could also use Objective-C objects in java? The
ability to use whichever language you want on a class-by-class basis
would be awesome . Finally, a package system breaks none of the theory
behind Objective-C, barely complicates the language, and could help get
around the stupid deprecation of #import in gcc. Of course, we would
probably need Garbage Collection in Objective-C to make it truly
seamless.... :).
Just a few thoughts.
On Friday, May 16, 2003, at 11:53 AM,
email@hidden wrote:
>
Generics can be useful in type-paranoid languages like C++. In dynamic
>
languages like Objective-C, Smalltalk, and Lisp, there's no point to
>
them. It's a shame they were added to Java for 1.5; they're not needed
>
in that language. I see their addition as a failure of the community
>
process, C++ developers clamoring loudly enough for something familiar
>
over something more powerful that they finally got their way. This is
>
something that I'd really, *really* like not to happen in Objective-C.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.