Last update November 15, 2003

DWiki /
First Class Functions

First Class Functions (DWiki)

The controversy as of 0.58 is: does D have first class functions or not?

From Structure and Interpretation of Computer Programs by Abelson and Sussmen

"In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. Elements with the fewest restrictions are said to have first-class status. Some of the 'rights and privileges' of first-class elements are:"

  • They may be named by variables.
  • They may be passed as arguments to procedures.
  • They may be returned as the results of procedures.
  • They may be included in data structures.
Sounds like a fair description of D's anonymous delegates. But the book does make a footnote:

"The major implementation cost of first-class procedures is that allowing procedures to be returned as values requires reserving storage for a procedure's free variables even while the procedure is not executing."

D does not do this. See /DynamicClosures.

From Microsoft's Vault language:

"First-class functions"

"Earlier, we said that C functions are statically declared; when a C program is running, no new functions are created. In Vault, however, a program can create new functions are run time. Some languages, like those in the Lisp family, provide this capability by making a compiler or interpreter available at run time through a library function, typically called "eval". Instead, Vault provides a more limited form of run-time function creation through the closure mechanism discussed above. For example:"

int() genfun() {

  static int seed = 0;
  int f() { return seed; }
  return f;

void main() {

  int() zero = genfun();
  int() one = genfun();
  int() two = genfun();
  printf("%d %d %d\n", zero(), one(), two());

"The function genfun creates a new function every time it is called. The first time you call it, it returns a function that always returns 0; the second time, it returns a function that always returns one; and so on. So, the output from this program is the message "0 1 2." A function like genfun cannot be portably written in C. This is the sense in which Vault functions are first-class and C functions are not."

By this definition, D's functions are not first-class either.

But D supports programming with /HigherOrderFunction which is a Good Thing.

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: November 15, 2003 4:45 (diff))