UFCS
   HOME

TheInfoList



OR:

Uniform function call syntax (UFCS) or uniform call syntax (UCS) or sometimes universal function call syntax is a
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
feature in D, Nim, Koka, and Effekt that allows any
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-orie ...
to be called using the syntax for method calls (as in
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
), by using the receiver as the first parameter and the given arguments as the remaining parameters. The same technique is used in the
AviSynth AviSynth is a frameserver program for Microsoft Windows, Linux and macOS initially developed by Ben Rudiak-Gould, Edwin van Eggelen, Klaus Post, Richard Berg and Ian Brabham in May 2000 and later picked up and maintained by the open source commu ...
scripting language under the name "OOP notation". UFCS is particularly useful when function calls are chained (behaving similar to
pipes Pipe(s), PIPE(S) or piping may refer to: Objects * Pipe (fluid conveyance), a hollow cylinder following certain dimension rules ** Piping, the use of pipes in industry * Smoking pipe ** Tobacco pipe * Half-pipe and quarter pipe, semi-circu ...
, or the various dedicated
operators Operator may refer to: Mathematics * A symbol indicating a mathematical operation * Logical operator or logical connective in mathematical logic * Operator (mathematics), mapping that acts on elements of a space to produce elements of another ...
available in
functional language In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map ...
s for passing values through a series of expressions). It allows free functions to fill a role similar to
extension method In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype, or a type. Extension methods are features of some object-o ...
s in some other languages. Another benefit of the syntax is related to completion systems in IDEs, which use type information to show a list of available functions, dependent on the context. When the programmer starts with an argument, the set of potentially applicable functions is greatly narrowed down, aiding
discoverability Discoverability is the degree to which something, especially a piece of content or information, can be found in a search of a file, database, or other information system. Discoverability is a concern in library and information science, many aspects ...
.


Examples


D programming language

int first(int[] arr) int[] addone(int[] arr) void main()


Nim programming language

type Vector = tuple[x, y: int] proc add(a, b: Vector): Vector = (a.x + b.x, a.y + b.y) let v1 = (x: -1, y: 4) v2 = (x: 5, y: -2) # all the following are correct v3 = add(v1, v2) v4 = v1.add(v2) v5 = v1.add(v2).add(v4)


C++ proposal

Proposals for a unification of member function and free function calling syntax have been discussed from the early years of C++ standardization. Glassborow (2004) proposed a uniform calling syntax (UCS), allowing specially annotated free functions to be called with member function notation. In 2016 it was proposed a second time for addition to C++ by
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, known for the development of the C++ programming language. He led the Large-scale Programming Research department at Bell Labs, served as a professor of computer sci ...
and Herb Sutter, to reduce the ambiguous decision between writing free functions and member functions, to simplify the writing of templated code. Many programmers are tempted to write member functions to get the benefits of the member function syntax (e.g. " dot-autocomplete" to list member functions); however, this leads to excessive
coupling A coupling is a device used to connect two shafts together at their ends for the purpose of transmitting power. The primary purpose of couplings is to join two pieces of rotating equipment while permitting some degree of misalignment or end mo ...
between classes. This was again, in 2023, proposed by Herb Sutter claiming new information and insights, as well as an experimental implementation in the cppfront compiler.


Rust usage of the term

Until 2018, it was common to use this term when actually referring to
qualified/explicit path syntax
' and most commonly the

': because it is possible to have several traits defining the same method implemented on the same struct, a mechanism is needed to disambiguate which trait should be used. Member functions can also be used as free functions through a qualified (namespaced) path. The term UFCS is incorrect for these uses, as it allows using methods as (namespaced) free functions, but not using free functions as methods.


See also

*
Trait (computer programming) In computer programming, a trait is a language concept that represents a set of methods that can be used to extend the functionality of a class. Rationale In object-oriented programming, behavior is sometimes shared between classes which are n ...
* Interface (computer programming) *
Go (programming language) Go is a high-level programming language, high-level general purpose programming language that is static typing, statically typed and compiled language, compiled. It is known for the simplicity of its syntax and the efficiency of development th ...
, another language with a more open philosophy to methods *
Loose coupling In computing and systems design, a loosely coupled system is one # in which components are weakly associated (have breakable relationships) with each other, and thus changes in one component least affect existence or performance of another comp ...
*
Duck typing In computer programming, duck typing is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose. With nominative ...
*
Method chaining Method chaining is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store th ...


References

{{Reflist Articles with example code Articles with example D code Object-oriented programming Subroutines