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] Koka, [3] and Effekt [4] 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. [5] The same technique is used in the AviSynth scripting language under the name "OOP notation". [6]

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, [7] 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. [8] In 2016 it has been proposed a second time for addition to C++ by Bjarne Stroustrup [9] and Herb Sutter, [7] 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); [10] however, this leads to excessive coupling between classes. [11] This has again, in 2023, been proposed by Herb Sutter [12] 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 [13] 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.

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

Lua is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications. Lua is cross-platform, since the interpreter of compiled bytecode is written in ANSI C, and Lua has a relatively simple C API to embed it into applications.

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 drawing inspiration from other high-level programming languages, notably Java, Python, Ruby, C#, and Eiffel.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

<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 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 some programming languages, const is a type qualifier, which indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in that it is part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time for each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

Managed Extensions for C++ or Managed C++ is a deprecated set of language extensions for C++, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the .NET Framework. These extensions were created by Microsoft to allow C++ code to be targeted to the Common Language Runtime (CLR) in the form of managed code, as well as continue to interoperate with native code.

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 the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called 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 (array) compares the features of array data structures or matrix processing for various computer programming languages.

C++17 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++17 replaced the prior version of the C++ standard, called C++14, and was later replaced by C++20.

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

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems 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. "The Koka Programming Language". koka-lang.github.io. Retrieved 2024-05-19.
  4. "Effekt Language: Introduction to Effekt". Effekt Language. Retrieved 2024-05-19.
  5. "Functions - D Programming Language". Dlang.org. Retrieved 1 October 2017.
  6. "Operators - Avisynth wiki". a.function(b) is equivalent to function(a, b)
  7. 1 2 ""Unified Call Syntax"" (PDF). Isocpp.org. Retrieved 1 October 2017.
  8. Francis Glassborow (2 May 2004). "N1585: Uniform Calling Syntax (Re-opening public interfaces)" (PDF). Retrieved 17 December 2018.
  9. ""UFCS proposal"" (PDF). Open-std.org. Retrieved 1 October 2017.
  10. "using intellisense". Msdn.microsoft.com. Retrieved 1 October 2017.
  11. "How Non-Member Functions improve encapsulation". Drdobbs.com. Retrieved 1 October 2017.
  12. Sutter, Herb (13 October 2023). "Unified function call syntax (UFCS)" (PDF).
  13. "Rename UFCS to accurately reflect its functionality. · Issue #1140 · rust-lang/rfcs". GitHub. Retrieved 2024-05-19.