Uniform Function Call Syntax

Last updated

Uniform Function Call Syntax (UFCS) or Uniform Call Syntax (UCS) or sometimes Universal Function Call Syntax is a programming language feature in D, [1] Nim, [2] Rust, [3] Koka, [4] and Effekt [5] that allows any function to be called using the syntax for method calls (as in object-oriented programming), by using the receiver as the first parameter and the given arguments as the remaining parameters. [6] The same technique is used in the AviSynth scripting language under the name "OOP notation". [7]

Contents

UFCS is particularly useful when function calls are chained [1] (behaving similar to pipes, or the various dedicated operators available in functional languages for passing values through a series of expressions). It allows free-functions to fill a role similar to extension methods 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, [8] aiding discoverability.

Examples

D programming language

intfirst(int[]arr){returnarr[0];}int[]addone(int[]arr){int[]result;foreach(value;arr){result~=value+1;}returnresult;}voidmain(){autoa=[0,1,2,3];// all the following are correct and equivalentintb=first(a);intc=a.first;// chainingint[]e=a.addone().addone();}

Nim programming language

typeVector=tuple[x,y:int]procadd(a,b:Vector):Vector=(a.x+b.x,a.y+b.y)letv1=(x:-1,y:4)v2=(x:5,y:-2)# all the following are correctv3=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. [9] In 2016 it has been proposed a second time for addition to C++ by Bjarne Stroustrup [10] and Herb Sutter, [8] 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); [11] however, this leads to excessive coupling between classes. [12] This has again, in 2023, been proposed by Herb Sutter [13] 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 [14] to use this term when actually referring to qualified/explicit path syntax and most commonly the Fully Qualified Path syntax: 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

Related Research Articles

In computing, a namespace is a set of signs (names) that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.

Generic programming is a style of computer programming in which algorithms are written in terms of data types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by the ML programming language in 1973, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplicate code.

<span class="mw-page-title-main">D (programming language)</span> Multi-paradigm system programming language

D, also known as dlang, is a multi-paradigm system programming language created by Walter Bright at Digital Mars and released in 2001. Andrei Alexandrescu joined the design and development effort in 2007. Though it originated as a re-engineering of C++, D is now a very different language. As it has developed, it has drawn inspiration from other high-level programming languages. Notably, it has been influenced by Java, Python, Ruby, C#, and Eiffel.

In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.

In computer science, a union is a value that may have any of multiple representations or formats within the same area of memory; that consists of a variable that may hold such a data structure. Some programming languages support a union type for such a data type. In other words, a union type specifies the permitted types that may be stored in its instances, e.g., float and integer. In contrast with a record, which could be defined to contain both a float and an integer; a union would hold only one at a time.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

In computing, an effect system is a formal system that describes the computational effects of computer programs, such as side effects. An effect system can be used to provide a compile-time check of the possible effects of the program.

In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.

In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole.

C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI to supersede Managed Extensions for C++. In December 2005, Ecma International published C++/CLI specifications as the ECMA-372 standard.

In the C++ programming language, argument-dependent lookup (ADL), or argument-dependent name lookup, applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call. This behavior is also known as Koenig lookup, as it is often attributed to Andrew Koenig, though he is not its inventor.

typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes.

In computer programming, redundant code is source code or compiled code in a computer program that is unnecessary, such as:

C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior version of the C++ standard, named C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.

In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. It is semantically related to the concept of a coroutine and is often implemented using similar techniques, and is primarily intended to provide opportunities for the program to execute other code while waiting for a long-running, asynchronous task to complete, usually represented by promises or similar data structures. The feature is found in C#, C++, Python, F#, Hack, Julia, Dart, Kotlin, Rust, Nim, JavaScript, and Swift.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level system programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. 1 2 "Programming in D - Universal Function Call Syntax (UFCS)". Ddili.org. Retrieved 1 October 2017.
  2. "Nim by Example - Procs". nim-by-example.github.io. Retrieved 2024-05-19.
  3. "Call expressions - The Rust Reference". doc.rust-lang.org. Retrieved 2024-09-22.
  4. "The Koka Programming Language". koka-lang.github.io. Retrieved 2024-05-19.
  5. "Effekt Language: Introduction to Effekt". Effekt Language. Retrieved 2024-05-19.
  6. "Functions - D Programming Language". Dlang.org. Retrieved 1 October 2017.
  7. "Operators - Avisynth wiki". a.function(b) is equivalent to function(a, b)
  8. 1 2 ""Unified Call Syntax"" (PDF). Isocpp.org. Retrieved 1 October 2017.
  9. Francis Glassborow (2 May 2004). "N1585: Uniform Calling Syntax (Re-opening public interfaces)" (PDF). Retrieved 17 December 2018.
  10. ""UFCS proposal"" (PDF). Open-std.org. Retrieved 1 October 2017.
  11. "using intellisense". Msdn.microsoft.com. Retrieved 1 October 2017.
  12. "How Non-Member Functions improve encapsulation". Drdobbs.com. Retrieved 1 October 2017.
  13. Sutter, Herb (13 October 2023). "Unified function call syntax (UFCS)" (PDF).
  14. "Rename UFCS to accurately reflect its functionality. · Issue #1140 · rust-lang/rfcs". GitHub. Retrieved 2024-05-19.