Skip to content

Commit aa6515b

Browse files
authored
Merge pull request #45 from mrhota/ufcs
Discuss unambiguous function call syntax
2 parents 0aa36de + 52b2635 commit aa6515b

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/expressions.md

+76
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ exact `self`-type of the left-hand-side is known, or dynamically dispatching if
220220
the left-hand-side expression is an indirect [trait
221221
object](types.html#trait-objects).
222222

223+
The compiler sometimes cannot infer to which function or method a given call
224+
refers. These cases require a [more specific syntax.](#disambiguating-function-calls)
225+
for method and function invocation.
226+
223227
## Field expressions
224228

225229
A _field expression_ consists of an expression followed by a single dot and an
@@ -544,6 +548,78 @@ let x: i32 = add(1i32, 2i32);
544548
let pi: Result<f32, _> = "3.14".parse();
545549
```
546550

551+
### Disambiguating Function Calls
552+
553+
Rust treats all function calls as sugar for a more explicit, fully-qualified
554+
syntax. Upon compilation, Rust will desugar all function calls into the explicit
555+
form. Rust may sometimes require you to qualify function calls with trait,
556+
depending on the ambiguity of a call in light of in-scope items.
557+
558+
> **Note**: In the past, the Rust community used the terms "Unambiguous
559+
> Function Call Syntax", "Universal Function Call Syntax", or "UFCS", in
560+
> documentation, issues, RFCs, and other community writings. However, the term
561+
> lacks descriptive power and potentially confuses the issue at hand. We mention
562+
> it here for searchability's sake.
563+
564+
Several situations often occur which result in ambiguities about the receiver or
565+
referent of method or associated function calls. These situations may include:
566+
567+
* Multiple in-scope traits define methods with the same name for the same types
568+
* Auto-`deref` is undesirable; for example, distinguishing between methods on a
569+
smart pointer itself and the pointer's referent
570+
* Methods which take no arguments, like `default()`, and return properties of a
571+
type, like `size_of()`
572+
573+
To resolve the ambiguity, the programmer may refer to their desired method or
574+
function using more specific paths, types, or traits.
575+
576+
For example,
577+
578+
```rust
579+
trait Pretty {
580+
fn print(&self);
581+
}
582+
583+
trait Ugly {
584+
fn print(&self);
585+
}
586+
587+
struct Foo;
588+
impl Pretty for Foo {
589+
fn print(&self) {}
590+
}
591+
592+
struct Bar;
593+
impl Pretty for Bar {
594+
fn print(&self) {}
595+
}
596+
impl Ugly for Bar{
597+
fn print(&self) {}
598+
}
599+
600+
fn main() {
601+
let f = Foo;
602+
let b = Bar;
603+
604+
// we can do this because we only have one item called `print` for `Foo`s
605+
f.print();
606+
// more explicit, and, in the case of `Foo`, not necessary
607+
Foo::print(&f);
608+
// if you're not into the whole brevity thing
609+
<Foo as Pretty>::print(&f);
610+
611+
// b.print(); // Error: multiple 'print' found
612+
// Bar::print(&b); // Still an error: multiple `print` found
613+
614+
// necessary because of in-scope items defining `print`
615+
<Bar as Pretty>::print(&b);
616+
}
617+
```
618+
619+
Refer to [RFC 132] for further details and motivations.
620+
621+
[RFC 132]: https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md
622+
547623
## Lambda expressions
548624

549625
A _lambda expression_ (sometimes called an "anonymous function expression")

src/undocumented.md

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ to shrink!
1414
specified.
1515
- [Flexible target specification] - Some---but not all---flags are documented
1616
in [Conditional compilation]
17-
- [Unambiguous function call syntax]
1817
- [Require parentheses for chained comparisons]
1918
- [Integer overflow not `unsafe`] - documented with a reference to the RFC, but
2019
requires further details

0 commit comments

Comments
 (0)