-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directly Embed Type Constraints in Cpp2 Type Definitions #1353
Comments
I assume for |
So this would essentially add a |
Yes, you are right. The idea behind this proposal is to insert a An example of this would be something like: point_3d :type = { x: ui64; y: ui64; z: ui64; }; However, in most cases, the types we define are more complex, with member functions and invariants that benefit from such constraints. For those cases, I believe handling this situation would be straightforward. |
The reason for inserting I'm not sure whether template specialization and partial specialization are already introduced in Cpp2, so the following code will be written in Cpp1 syntax: template <typename T, typename U>
struct Printer;
template <typename T, typename U>
struct Printer
{
void print();
};
template <typename T>
struct Printer<T, int>
{
void print();
};
template <>
struct Printer<int, int>
{
void print()
{
static_assert(printable_0<Printer<T, U>>);
}
};
template <typename T>
struct Printer<T, T>
{
void print();
};
template <typename T, typename U>
void Printer<T, U>::print()
{
static_assert(printable_1<Printer<T, U>>);
}
template <typename T>
void Printer<T, int>::print()
{
static_assert(printable_2<Printer<T, int>>);
}
template <typename T>
void Printer<T, T>::print()
{
static_assert(printable_3<Printer<T, T>>);
} As shown above, we can apply different constraints for partial and full specializations. However, I'm not sure whether this level of granularity is necessary—perhaps we don't need to apply such fine-grained self-imposed constraints. It might be sufficient for all specializations to share the same concept. |
You could probably reuse the recently introduced syntax of |
Summary:
I propose introducing a feature in Cpp2 that allows users to express constraints on types themselves directly in the type definition. This could be done using the syntax
name:type:concept={...}
orname:type is concept = {...}
, allowing the defined type to automatically satisfy specific constraints (concepts).Problem:
Currently in Cpp2, we can constrain deduced types (e.g.,
number: _ is std::regular = ...
), but there is no straightforward way to enforce constraints on the type itself. This feature would close that gap and enable a more declarative way to ensure types meet certain requirements.Proposed Syntax:
Current Cpp2 Syntax:
This generates the following Cpp1 code:
New Feature:
The proposed feature would allow adding constraints directly to the type definition:
This would generate the following Cpp1 code with a
static_assert
to ensure thatPoint
satisfies thedrawable
concept:Benefits:
Self-Documenting:
The constraint directly on the type definition acts as documentation. It makes the type's requirements clear and self-contained, so readers immediately understand what the type should satisfy without needing to refer to separate documentation.
Declarative Constraints:
This feature allows constraints to be declared directly within the type definition, improving code readability. Instead of adding
static_assert
or other checks manually in the implementation, constraints are declared in one place, making the code more concise and less error-prone.Aligns with Existing Features:
Cpp2 already supports type constraints on deduced types using the syntax
name: _ is concept = ...
. Extending this syntax to constrain the type itself ensures consistency within the language and provides a more flexible and expressive way to enforce type contracts.The text was updated successfully, but these errors were encountered: