Forgive me if this topic has been brought up before. I can’t find anything via search.
With the introduction of constant generics and more broadly the notion of constant expressions and evaluation, it follows that there should be some method to apply constraints to const generics in a manner similar to how constraints are placed on type parameters.
For a motivating example, it would be nice to define structs such as this in as generic of a way possible:
struct Vector<T, const ARITY: usize>
where
{ARITY > 0};
Another way this could be done is through a bunch of boiler plate in a manner similar to:
This does not currently work, and in fact the compiler error generated by this (the trait bound Bool<{N > 0}>: True is not satisfied) conforms to the text of the const generic PRD.
Currently only the bare bones of const-generics are implemented. Also, (currently) all expressions are treated as a black box, so {x+1} is not the same as {x+1} in two different locations. This might throw a wrench in your plans until const-generics becomes more powerful.
yes, it would be quite some time before anything like this could be implemented at all. I’m not posting an RFC or anything for that reason.
The purpose of this is to spur discussion into whether or not the feature is desirable, and if it is desirable, to spur discussion into what the syntax and semantics for such a feature would be
Indeed, nothing is lost in terms of optimization ability. Constant evaluation in where clauses is basically intended to be a static assertion to avoid certain potentially buggy or otherwise useless or illogical implementations.