switch expression involving constant expressions should be considered constant
#7489
Unanswered
Rekkonnect
asked this question in
Language Ideas
Replies: 2 comments 12 replies
-
|
For your last two examples, I don't believe ternary works this way. Everything involved needs to be constants, and I don't see the point in doing otherwise. |
Beta Was this translation helpful? Give feedback.
7 replies
-
|
Won't this also just come with warnings about unreachable branches? |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Following #6925, which is championed in #6926
Summary
When evaluating and returning only constant expressions, using a
switchexpression is not considered constant. This can be safely adjusted.Motivation
Conditional compilation symbols, reliance on runtime constants deriving from conditional statements
Description
You cannot assign a
switchexpression only involving constant values to a constant symbol. For example,In the example above, the evaluated expression (
A) is a constant, and the returned expressions are also constant.Any throw expression would result in a compiler error, saying "the constant result evaluates into throwing an exception". If the throw expression is not reached, there will be no compiler error.
If any non-constant expression is returned, even when the resulting branch does not reach it, the entire switch expression is considered to not be constant, and a specific compiler error would be returned showing that some expressions are not constant.
Example
In the following example:
The result would be
Bhaving the value 1 ifA is <= 6 and not 1 and not 2. Otherwise, a compiler error would be thrown indicating that the resulting branch is a throw expression.In the following example:
The arm for the
> 6condition does not return a constant expression, and so the entire switch expression is considered non-constant.Beta Was this translation helpful? Give feedback.
All reactions