I would argue that extension traits (that exist solely to be implemented by a single impl
block) are underused specifically because of the extra weight that it takes to provide method syntax. Since function syntax is always sufficient, many potential users of extensions will just write functions instead outside of applications where method chaining and/or receiver autoref is highly impactful.
While trait
s are the mechanism, what extensions actually are expressing is a way to write methods which avoids the problems that coherence exists to prevent. If you include "why can't I write an impl
for this type" as well as usage of extension style traits, I think the utility threshold is easily passed.
And also FWIW, phrased in my preferred shape of "free methods" (detailed below) instead of sugar for "extension traits" the feature fits fairly well into the "make existing things just work in more cases" box and is thus a removal of a restriction (methods must always be associated functions) as much as it is an extension to language sugar.
I don't really think so; some combination of trait
and impl
is reasonable. At a language level this feature is combining the trait
definition with the impl
block and so that's what the syntax should say. Like how newtypes are just struct
and not some additional keyword to say "this is a newtype style structure."
My personal favorite concept, however, is to just allow directly writing free methods, e.g.
fn f(self: &Command, …) -> … {
…
}
then when f
is in scope, it's available to method lookup on the Command
type. That it's a method is determined by the use of a self
parameter.
What is perhaps a bit interesting to ask in this case is what happens when you use f as g
. It could then be available as cmd.g()
, or it could behave like the trait version does and still provide cmd.f()
. Whether it supports use f as _
to only be available to method lookup is also a question.
In the case where it's just a function instead of a trait, it's just f(&cmd)
for disambiguated function call syntax instead of the more involved qualified UFCS.
And to that note, qualified UFCS is another potential option to address the underlying want here, e.g. allowing to write something like cmd.(path::to::f)()
to call an item-path-lookup function with method syntax (and all that entails, e.g. autoref) instead of always needing to insert names into method lookup.