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

Generic declarations

Prev: Compile-time variadics


Next: True Generic Types

Like several languages before it, notably Ada and C++, XL features generic entities.

From a semantics point of view, XL generics are closer to C++ templates than to Ada generics. In particular, XL features implicit and transitive instantiation, which are a key requirement for advanced template libraries similar to the C++ STL. Instead of using the < separator for generic parameter or arguments, XL uses a block notation delimited with angle brackets [ ].

// Simple example of generic type declaration generic [index : range; value : type] type array written array[index] of value Cards : array[1..52] of card Frequency : array['A'..'Z', integer]

// Example of generic function declaration (see a better definition) generic [value : type] function Max (X, Y : value) return value is if X < Y then return Y else return X

There are a few aspects of XL generics that might surprise C++ or Ada programmers.

  • XL uses generics for types that are built-in in other languages, like pointers, arrays, strings or lists.
  • XL generics are typically defined with a written form to offer a convenient syntax, as illustrated by the array type above.
  • XL generic types can be used much like regular types.
  • XL can validate generics. Validated generic types can represent concepts common in the C++ STL, such as ForwardIterator. By contrast, in C++, everybody but the compiler knows what a ForwardIterator is.

Variadic entities are also considered a form of generic.

Note: At this stage, only the C++ version of the XL compiler has any kind of support for generics.

Prev: Compile-time variadics


Next: True Generic Types

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