Tags

, ,

A bane of programming is repeating code and dealing with a myriad of types. The natural response to type overload is to limit the number of types. This runs counter to the intent of types however, as they are there to help us. Something that makes us avoid them is bad. Yet having to write specialized code for different types is always a hassle. This is where templates come in: they allow you to write one piece of code that applies to a lot of different types.

Example

The idea of templates and auto-typing are actually very closely related. It really does appear that if one took both to their extreme it would end up being one feature. Consider this simple example.

int op( int a, int b)
{ return a + b; }

int q = op( 2, 3 );

op should really be a template since it can apply to any types which can be added together. We could start sticking the word auto in everywhere, but it makes sense to just allow the following instead.

op( a, b )
{ return a + b; }
q = op(2,3);

Just let the compiler figure out what types are involved.

Implications

With complete auto-typing the standard template capabilities like collections and parametric functions become a natural part of the language. The danger here, if we look at C++, is that all the code has to remain inside header files and be available to all units that need to use it. This issue will have to be addressed: somehow a packaging system will have to allow code to reside in its own file.

For interfacing with other languages and systems some way to export specific versions of the functions, and possibly classes, will be required. This further implies that the compiler will actually be capable of generating the typed code and not just generic code. For performance reasons this is a boon. However, if every minor variation of a type produces new code it will produce too much code. So an intelligent compiler will somehow have to know when it can reuse the same code.

Questions

Does such a requirement imply up-typing or just down-typing. Up-typing means the type to which the return of a function is assigned can influence the typing on the function itself. This sounds great and has many practical uses, but has one major drawback: to implement up-typing you need a constraint system in the compiler. A constraint system however requires an extreme amount of memory and processing time to produce an actual stable solution. So while desired, it may not be feasible. Perhaps a limited form would be possible.

Obviously this just scratches the surface of auto-typing. A great deal of more definition and clarification will be required to produce a usable solution.

 

Advertisements