This is the stabilization report for const parameter defaults and the removal of the type and const parameter ordering restriction. Its feature is #![feature(const_generics_defaults)]
.
This report is a collaborative effort of @BoxyUwU and @lcnr.
While supplying default arguments for type parameters is already possible on stable, this is not allowed for const parameters. Due to the restriction that type parameters must be in front of all const parameters, type parameter defaults cannot be used on stable if a const parameter exists.
This stabilization allows type and const parameters to be in an arbitrary order and adds the ability to specify default values for const parameters in all places where this is already allowed for type parameters. These defaults have the same restriction as other const arguments, so they must either be a fully concrete expression or another const parameter.
Const parameter defaults
Removal of the ordering restriction
T = 32
) and a const generic (const N: u8
) without removing the ordering restrictionThe ordering restriction between types and consts is removed. The ordering of generic params is now: lifetimes, then type and const parameters.
We previously restricted the ordering due to implementation concerns, which have since been fixed.
It is currently possible to provide a default type to type parameters in type and trait definition. With this stabilization it becomes possible to provide default values for const parameters as well.
The used syntax is const PARAM_NAME: Ty = expr
where expr
is either a single segment path, a literal, or surrounded by braces, e.g. { foo() }
. This restriction is identical to the restriction of const parameters and simplifies parsing.
Const defaults, for now, are not permitted to involve computations depending on generic parameters. This means that defaults may only be:
{ foo() + 1 }
, where foo
is a const fn.N
. Note that both type and const parameter defaults may only refer to preceeding parameters, meaning that struct Foo<const N: usize = M, const M: usize = 3>
causes an error.When using an expression which does not evaluate successfully, e.g. usize::MAX + 1
, as a const parameter default, we eagerly emit an error. This also mirrors the behavior of type parameter defaults.
The Book: Does not contain any documentation about const generics
The Reference: reference#1098
usize::ASSOC
is resolved today, but will be ambiguous if we start providing where clauses to type level constants. This is not a new issue however, as the following code has the same issue and currently compiles on stable:
We therefore believe that this issue should not block the stabilization of const parameter defaults.