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); var foo : TMyParentClass; begin foo := TMyChildClass.Create; DoSomeProcess(foo)
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:
var p : Pointer; begin DoSomeProcess(TMyChildClass(p));
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: