Home | About | Partners | Contact Us

SourceForge Logo

Quick Links
Building XL
XL Mailing List

Understanding XL
Conceptual overview
XL examples
Inside XL
Concept Programming

In depth
Browse GIT
SourceForge Info

Other projects
GNU Project
The Mozart Project

XLR: Extensible Language and Runtime

The art of turning ideas into code

True Generic Types

Prev: Generic declarations


Next: Generics validation

Frequently, when you declare a generic type, you also declare a number of related generic declarations (other types, algorithms, etc). Before the C++ STL, we had little experience with large bodies of generic code.

But the STL demonstrated how verbose code becomes when all generic parameters need to be repeated over and over. Consider for instance an array generic type, which depends on a value type (what you store in the array) and an index type (how you index the array). Consider now that you want to implement the minimum, maximum and a sort operation on this array. The C++ declarations (ignoring the actual implementations) will look like:

// Example C++ template code template <typename Index, typename Value> class Array;

template <typename Index, typename Value> Value Max(const Array<Index,Value> &A);

template <typename Index, typename Value> Value Min(const Array<Index,Value> &A);

template <typename Index, typename Value> void Sort(Array<Index,Value> &A);

Obviouly, there is a lot of syntactic noise in this example, and the signal/noise ratio is a bit low. This is why XL allows generic types to be used as regular types, in which case the declaration they are contained in becomes generic implicitly. In such declarations, the arguments of the original generic type can be referred to using a dotted notation. The exact XL equivalent of the above code would therefore be:

// Equivalent XL generic code generic [type index; type value] type array function Max(A : array) return array.value function Min(A : array) return array.value to Sort (in out A : array)

True generic types are even more interesting when they are validated

Note: At this stage, only the C++ version of the compiler implements generics.

Prev: Generic declarations


Next: Generics validation

Copyright 2008 Christophe de Dinechin (Blog)
E-mail: XL Mailing List (polluted by spam, unfortunately)