请输入您要查询的百科知识:

 

词条 Uniform Function Call Syntax
释义

  1. C++ proposal

  2. Examples

      D programming language    Nim programming language  

  3. Rust usage of the term

  4. See also

  5. References

{{Primary sources|date=September 2017}}

Uniform Function Call Syntax (UFCS) or Uniform Calling Syntax (UCS) or sometimes Universal Function Call Syntax is a programming language feature in D and Nim 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.[1] UFCS is particularly useful when function calls are chained[2] (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 method call syntax is use with "dot-autocomplete" 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,[3] aiding discoverability.

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.[4]

It has more recently been proposed (as of 2016) for addition to C++ by Bjarne Stroustrup[5] and Herb Sutter,[3] 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);[6] however, this leads to excessive coupling between classes.[7]

Examples

D programming language

import std.stdio;

int first(int[] arr)

{

}

int[] addone(int[] arr)

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

}

void main()

{
    // All the following are correct and equivalent    int b = first(a);    int c = a.first();    int d = a.first;
    // Chaining    int[] e = a.addone().addone();

}

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)   v3 = add(v1, v2)  v4 = v1.add(v2)  v5 = v1.add(v2).add(v1)

Rust usage of the term

Until 2018 it was common to use this term when actually referring to [https://github.com/rust-lang/rfcs/issues/1140#issuecomment-108644620 qualified/explicit path syntax] and most commonly the [https://doc.rust-lang.org/book/2018-edition/ch19-03-advanced-traits.html?highlight=trait,function,call#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name Fully Qualified Path syntax].: because it's 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)
  • interface (computer programming)
  • Go (programming language), another language with a more open philosophy to methods
  • Loose coupling
  • Duck typing

References

1. ^{{cite web|url=http://dlang.org/function.html#pseudo-member|title=Functions - D Programming Language|website=Dlang.org|accessdate=1 October 2017}}
2. ^{{cite web|url=http://ddili.org/ders/d.en/ufcs.html|title=Programming in D - Universal Function Call Syntax (UFCS)|website=Ddili.org|accessdate=1 October 2017}}
3. ^{{cite web|title="Unified Call Syntax"|url=https://isocpp.org/files/papers/N4165.pdf|format=PDF|website=Isocpp.org|accessdate=1 October 2017}}
4. ^{{cite web|title=N1585: Uniform Calling Syntax (Re-opening public interfaces)|author=Francis Glassborow|date=2 May 2004|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1585.pdf|accessdate=17 December 2018}}
5. ^{{cite web|title="UFCS proposal"|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4174.pdf|format=PDF|website=Open-std.org|accessdate=1 October 2017}}
6. ^{{cite web|title=using intellisense|url=https://msdn.microsoft.com/en-us/library/hcw1s69b.aspx|website=Msdn.microsoft.com|accessdate=1 October 2017}}
7. ^{{cite web|title="How Non-Member Functions improve encapsulation|url=http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197|website=Drdobbs.com|accessdate=1 October 2017}}

4 : Object-oriented programming|Source code|Subroutines|Articles with example code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/22 20:21:31