Movatterモバイル変換


[0]ホーム

URL:


DLang LogoDLang Tour
Menu

Uniform Function Call Syntax (UFCS)

UFCS is a key feature of D and enables code reusabilityand scalability through well-defined encapsulation.

UFCS allows any call to a free functionfun(a) to be written as a member function calla.fun().

Ifa.fun() is seen by the compiler and the type doesn'thave a member function calledfun(), it tries to find aglobal function whose first parameter matches that ofa.

This feature is especially useful when chaining complexfunction calls. Instead of writing

foo(bar(a))

It is possible to write

a.bar().foo()

Moreover in D it is not necessary to use parentheses for functionswithout arguments, which means thatany function can be usedlike a property:

import std.uni : toLower;"D rocks".toLower; // "d rocks"

UFCS is especially important when dealing withranges where several algorithms can be combinedto perform complex operations, making iteasier to write clear and manageable code.

import std.algorithm : group;import std.range : chain, retro, front, dropOne;[1, 2].chain([3, 4]).retro; // 4, 3, 2, 1[1, 1, 2, 2, 2].group.dropOne.front; // tuple(2, 3u)

In-depth

rdmd playground.d


[8]ページ先頭

©2009-2025 Movatter.jp