- Start Date: 2015-01-22
- RFC PR: rust-lang/rfcs#587
- Rust Issue: rust-lang/rust#21527
Summary
The Fn
traits should be modified to make the return type an associated type.
Motivation
The strongest reason is because it would permit
This impl is currently illegalR
is not constrained. (This also has an impact on my attempts to add variance, which would require a "phantom data" annotation for R
for the same reason; but that RFC is not quite ready yet.)
Another related reason is that it often permitsF::Output
can be used. ConsiderMap
type:
This type could be equivalently written:
This example highlights one subtle point about the ()
notation,
Detailed design設計(する)
The designFn
trait is modified to read as follows:
The other traits are modified in an analogous fashion.
Parentheses丸かっこ(で囲う) notation記法
The shorthandFoo(...)
expands to Foo<(...), Output=()>
. The shorthandFoo(..) -> B
expands to Foo<(...), Output=B>
. This impliesFn
traits is currently feature-gated, as described
This can be seen in the In the Map
example from the introduction.<>
notationF::Output
is left unbound:
An alternativeB
:
Or to remove the boundF
from the type definition
Note that this final option is not legalB
on the impl would be unconstrained.
Drawbacks
Cannot overload based基となる、基底(の) on return type alone
This change means that you cannot overload indexing to "model" a trait like Default
:
That is, I can't do something like the following:
This is not possible because the impl type parameterT
is not constrained.
This does not seem like a particularly strong limitation. Overloaded call!
notationFnMut() -> !
is not legal). The ability to overload based
Alternatives
Special syntax文法 to represent the lack of an Output
binding
Rather than having people use angle-brackets to omitOutput
binding, we could introduceFnMut() -> ?
could desugar to FnMut<()>
(whereas FnMut()
alone desugars to FnMut<(), Output=()>
). The first suggestion that is commonly made is FnMut() -> _
, but that has an existing meaning in a function context_
represents
Change meaning of FnMut()
to not bind束ねる/くっつける the output
We could make FnMut()
desugar to FnMut<()>
, and hence require an explicitFnMut() -> ()
to bind