-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
This page notes that because casting uses an identifier surrounded by parentheses, some expressions may be ambiguous. It's not clear, though, what implication ambiguous syntax has for a programmer. Is there a possibility of the compiler interpreting it one way, but later being updated to interpret it another way? Should the developer take care when using third-party compilers, such as that for Mono? Or is the interpretation well-specified for any ECMA-334 compliant compiler?
For the example given, (x)-y, it looks like the current behavior (VS 2017 15.5.5, C# 7.2) actually does disambiguate the cases:
- If identifiers
xandyboth denote variables, there is no compiler error or warning, and the expression is treated asx - y(y subtracted from x). - If identifier
xdenotes a type andydenotes a variable, the compiler emits error CS0075 (even if an explicit conversion exists). This error cites the language specification section on cast expressions, which repeats this example and notes that there is a disambiguation rule that the compiler is following.
Given that there is a disambiguation rule in the spec, should this page even mention that there could be an ambiguity? It seems like "ambiguous grammar" is an implementation detail resolved by an additional rule on the compiler. Or is there another reason to caution developers when using the cast operator?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 2fe11a11-230c-eebb-9a64-e22d5d73094d
- Version Independent ID: 05d2b6e1-d471-7baa-ed9e-79d72d99bd4a
- Content: () Operator (C# Reference)
- Content Source: docs/csharp/language-reference/operators/invocation-operator.md
- Service: unspecified
- Product: dotnet-csharp
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn