|
|
|
|
Quick Links
Understanding XL
In depth
Other projects
|
XLR: Extensible Language and Runtime
|
A frequent need in the representation of a particular concept is the notion that a same operation applies to a set of entities, the size of set not necessarily being known ahead of time. A very common case is text I/O, where writing multiple elements is as common, if not more, than writing a single one. Another example is the computation of a maximum, minimum or average of a set of values. A mathematician would denote that as max(x,y,z) for instance. Historically, there have been a number of ways to address this particular problem. They all introduce significant semantic noise, which manifests in the form of run-time inefficiency or other undesired properties:
XL takes a different approach for variadics. A variable parameter list can be declared using other at the end of a parameter list. The other parameter stands for all other arguments. In the body of the variadic entity, other can be used as an argument, and it is replaced with the actual list of arguments. Properly used (typically, by "shaving off" one argument at a time using a regular parameter), this mechanism allows a recursive instantiation of all the required entities.
// Example of variable argument max for integers // See a more general implementation here function max(X : integer) return integer is return X function max(X : integer; other) return integer is result := max(other) if result < X then result := X The expansion is performed entirely at compile-time, so there is no run-time cost associated with list management. At every step in the recursion, the type system is used to make sure that the argument types are corrects, and the technique is compatible with overloading. The same technique can be used for text I/O as well as for maximum or average functions, and it doesn't require the creation of any intermediate object (containers, boxing or unboxing). For these reasons, the XL approach to variadics creates less semantic noise that alternatives. Note: At this point, variadics only work in the C++ version of the XL compiler.
|
Copyright 2006 Christophe de Dinechin (Blog)
E-mail: XL Mailing List (polluted by spam, unfortunately)