miércoles, agosto 18, 2004

Generics and more generics

While waiting for the GC Book Federico will lend me in a marvelous action, I'm working at Generics, a new feature that will be part of the Mono 1.2 release for next february and that will have compatibility with the .Net 2.0 from MS.

Currently I'm working implementing some classes in the System.Collections.Generic, specially in the Collection class, that it is designed to be the base for custom classes that need to keep a collection of objects, thus avoiding writing a new collection class from scratch. It is a small and easey-to-implement class, however, this is a great chance to code Generics and learn a lot of them. I highly recommend the document in the Generics site to understan the way they work and the internals to make this feature happen.

Some notes about Generics
There may be some people who doesn't want to go and read that paper, because of its complexity or because its size. For them, some notes about thus new feature called Generics, or Parametric Polymorphism.

Currently the collection classes (Stack, Queue, ArrayList, etc) use System.Object as a wrapper for the data (this could be the equivalent to void* in C world, but not exactly the same). However, this impact a lot in performance, beacuse when you use reference-based variables (instance classes, e.g. System.String), the runtime will have to 'wrap' your variable. Something worse happen with value-based variables (e.g. System.Int, System.Long, System.Enum, etc), because they originally live in an area called 'runtime stack', that make them very light, and thus casting them to be of type System.Object will create a reference-based variable, using more innecessary space and also creating an almos useless additional variable.

Then, when a value based variable is passed as argument for a method, it will be automatically casted and impact performance. When an instance class is passed, the impact will be smaller, but it will exists. Obviously, this can cause some coders to re-implement a custom collection class.

Because of that, Parametric Polymorphism exists. It is a feature similar to those 'templates' in C++ world (note that I'm not a C++ guru). As far as I know, templates is a mechanism similar to macros in C world, but better. Now, imagine you can use the capabilites of the CLI (Just-In-Time compilation, Metadata, etc), and then you will have an interesting design, creating custom classes based on the 'generic' class. So, everytime a class is requested, the runtime will check for it: if it previously exists, it will re-take the stubs; if not, it will create it.

Imagine the code:

class GenericClass <T> {
int Add (T item) {
...
}
}


Observe the new parameter, ''. The 'T' will be an alias for the types passed in the creation of the new class. This alias will be used along all the class definition and then will generate it depending on the type. For example, you could declare it as:

GenericClass <int> gclass = new GenericClass <int> ();
...
Add (6);

The above code will be valid, because the int type will be used as the base class,
having emitted code like:

int Add (int item) {...


Im my opinion, this is a great feature, and I recommend again the lecture of the paper that is on the Generics site.

2 comentarios:

Roberto Iza Valdes dijo...
Este blog ha sido eliminado por un administrador de blog.
Roberto Iza dijo...
Este comentario ha sido eliminado por el autor.