Skip to content
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

Const asserts #625

Open
ds84182 opened this issue Oct 15, 2019 · 2 comments
Open

Const asserts #625

ds84182 opened this issue Oct 15, 2019 · 2 comments
Labels
enhanced-const Requests or proposals about enhanced constant expressions

Comments

@ds84182
Copy link

ds84182 commented Oct 15, 2019

Today I encountered an issue where I needed to ensure that some constants are edited correctly to prevent a runtime error. While it's possible to write a StaticAssert class like this:

class StaticAssert {
  const StaticAssert(bool value, [String message = 'Assertion failed']) : assert(value, message);
}

Using it is somewhat difficult, since the analyzer complains about unused variables:

const _asserts = [
  _StaticAssert(a <= b),
  _StaticAssert(c <= b),
];

It would be a bit better if you could write a top level assert:

const assert(a <= b);
const assert(c <= b);

Which is sorta like static_assert in C++11.

@munificent
Copy link
Member

Your StaticAssert class is a really clever trick to approximate static_assert. :)

What kind of real code are you using this for? Since the expression itself must be constant, I'm having a hard time imagining practical uses for it.

@ds84182
Copy link
Author

ds84182 commented Oct 17, 2019

We have a couple of animation durations in milliseconds. We want to ensure that our max duration constant is >= all of the durations. Since there's no good way to do a compile time 'max' other than nested ternaries, we use static assert to make sure none of the constants were changed to be larger than the max duration, which would cause a runtime error.

@eernstg eernstg added the enhanced-const Requests or proposals about enhanced constant expressions label Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhanced-const Requests or proposals about enhanced constant expressions
Projects
None yet
Development

No branches or pull requests

3 participants