Keyword generics #162
Labels
final-comment-period
The FCP has started, most (if not all) team members are in agreement
major-change
Major change proposal
T-lang
to-announce
Not yet announced MCP proposals
Proposal
Summary and problem statement
When writing a function taking a
&mut
argument, it frequently (see*_mut
functions in libstd) happens that one also creates a (near-)duplicate function just with an immutable reference. The same is true forasync
functions,const
functions and various other keywords. If one were to combine these keywords, it is not too far fetched that one has anasync fn async_get_mut(&mut self)
, anasync fn async_get(&self)
, afn get_mut(&mut self)
and afn get(&self)
function. Adding more variants of the same function for other keywords duplicates the number of functions for each keyword.Motivation, use-cases, and solution sketches
For
const
, the rust-lang/rfcs#2632 was accepted for experimentation to figure out this problem. That RFC even added explanatory (never to be stabilized) syntax to explain the feature in a "constness generic" way. Basically we would like to make functions likeOption::unwrap_or_default
a const fn. In order to do this, we need to make sure that the generic parameterT
has aDefault
impl that can be evaluated at compile time. But at the same time, we want the function to be usable with types that haveDefault
impl that is not evaluable at compile time, as long as the function is not called from a const context (This is what is allowed on stable today, as there are no const fn with generic parameters that can be invoked). The RFC spelled this out as "const if const" trait bounds, so at present (unstably), we could writewhere
~const
is a placeholder syntax meaning "const if const" (If you have seen the RFC, this used to be the opposite, where?const
meant "unknown if const, treated as not const" and the "const if const" part was implied everywhere else).In the explainer syntax from the RFC this was written as
Where you can select at the call site whether you get a
const fn
or a plainfn
by changing theC
parameter (or with some sugar it is chosen automatically for you).We would like to explore these keyword generics in a shared way, instead of just experimenting more with
const fn
. We don't know the syntax yet and the full scope, but in https://github.com/yoshuawuyts/keyword-generics-initiative we're starting to track the ideas for this feature.Links and related work
const
: [WIP] Rework the entire const trait system rust#96077Initial people involved
What happens now?
This issue is part of the lang-team initiative process. Once this issue is filed, a Zulip topic will be opened for discussion, and the lang-team will review open proposals in its weekly triage meetings. You should receive feedback within a week or two.
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
The text was updated successfully, but these errors were encountered: