Jump to content

Uniform function call syntax

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Behnam (talk | contribs) at 17:15, 13 October 2014 (Create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Uniform Function Call Syntax (UFCS) is a programming language feature in D that allows any function to be called on an object (as in Object-oriented programming) like the function is a method of its class.[1] UFCS is particularly useful when function calls are chained.[2]

Examples

import std.stdio;

int first(int[] arr)
{
    return arr[0];
}

int[] addone(int[] arr)
{
    int[] result;
    foreach (value; arr) {
        result ~= value + 1;
    }
    return result;
}

void main()
{
    auto a = [0, 1, 2, 3];

    // All the followings are correct and equivalent
    int b = first(a);
    int c = a.first();
    int d = a.first;

    // Chaining
    int[] e = a.addone().addone();
}

References