Friday, July 09, 2004

Re: Unified Type System

I was a bit confused about what you are trying to say? Do you mean simple Casting? How is C++ better at handling collections/generics w/o unified type system?

I was saying because there is a hierarchy with Object being the root, the Collection classes (just as an example) can work with just Objects and not have to worry about other classes. If they didn't have this hierarchy then you would need a StringArrayList, IntArrayList, MyClassArrayList etc... In other words, specializations for every possible class you would want to put into any collection. Now, if they had introduced the framework with generics, you could have just done ArrayList<String>, ArrayList<Int>, ArrayList<MyClass> and they would NOT need to inherit from Object. This is how it is in C++.

To notice the advantages of Object look at the class itself. Object guarantees that Thread support is provided out of the box with final classes like wait( ) notify( ). Other methods like clone( ) equals( ) getClass( ) and hashCode( ) provide features to be extended by child classes. getClass( ) is important for Reflections. hashCode( ) is important for generating hashcode indices for Collections like Map. Also finalize( ) serves as a Destructor.

I don't see this as an advantage. If your class never needs to be output to console (or whereever), you won't need the ToString() method. Similarly for Clone(), if you won't ever make a copy of yourself or Equals() if you will never be compared. C++ gives you the option. If you want to output to console (or whereever) you overload operator<<. Same for equals and clone... overload operator== and copy construtor. You are in control. There is no default overhead.

You even could argue why they didn't make CompareTo() a method in Object. Why did they choose to go for an interface Comparator (IComparator in .NET)? C++ has a similar way of doing it. Not with interfaces, but functors. If you want your object to be sorted, you create a functor, which performs the comparison, and pass it to an algorithm that sorts the collection.

I think I may have drifted quite a bit, but hey.. it is a pvt blog!! Now can any of you verify this?

Drift as much as you like... It's all good.

We discussed casting a while back. Check out the archives. It's pretty much exactly as you said it.

Do you know how it happens in C++?

I would think it's the same, except there is no Object at the top. In Java and .NET, it's ALWAYS there.

I thought this had to do in a Major way with garbage collection too

You might be right, but it would depend on implementation details. When a garbage collection is invoked, it creates a graph of all reachable objects from the roots in the application. It marks all those that are NOT reachable and destroys them. I guess this is called the mark and sweep collector. Maybe during the mark phase, an implementation would change a flag in the objects to say that it is garbage. If this were the case, then, yeah, Object would play a role.

But, I'm skeptical since there are garbage collectors out there for C++ and we all know C++ doesn't have a Unified Type System. Even Java allows for different garbage collectors. So I wouldn't think that Object would play a role in it.

Not sure though, a detailed answer by someone would be welcome.

I'm quite confused about this. I don't think any of us has hit the nail.

No comments: