5 Oct 2007 jnewbigin   » (Apprentice)

Automatic type conversion

I just added my 2c about Inner Classes to the WIKI but it got me thinking again about a feature I think is lacking in modern languages. Having slipped out of the top 10 blogs, I felt it was time to share my idea.

The feature is automatic type conversion. When you have some pseudo code like this:

class TMyChildClass inherits from TMyParentClass;
function DoSomeProcess(bar : TMyChildClass);
 foo : TMyParentClass;
 foo := TMyChildClass.Create;

It does not work. The problem is that although foo is compatible, the compiler wants me to explicitly say so. The solution is to add an explicit type conversion like this


But this becomes a problem if the conversion is not type checked until run time. If for example I had:

 p : Pointer;

I have valid code but I get a run time error because of the invalid type cast. So, the explicit type cast is for shutting up the compiler, not for making my code correct.

The compiler complains because it knows what type to expect, and given that it knows, I think it should be able to implicitly cast my parameter like this:


and I get the same result. If the type is correct, it works and if not I get a run time error.

What about preventing programming bugs? Well I guess some kind of syntax is required to allow the automatic conversion. Each language would have to pick it’s own syntax but in delphi, it might be something like this:

DoSomeProcess(auto foo);

Syndicated 2006-05-15 01:13:50 from John's test blog

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!