I'm having a go at implementing C# spec 7.16.2 "Query expression translation" in Roslyn. However, I've run into a problem in 7.16.2.5 "Select clauses".
It reads
A query expression of the form
from x in e select v
is translated into
( e ) . Select ( x => v )
except when v is the identifier x, the translation is simply
( e )
For example
from c in customers.Where(c => c.City == "London") select c
is simply translated into
customers.Where(c => c.City == "London")
My code does not produce a result matching the example, because (as per the "except when" line) I translate from x in e select x
into ( e )
, rather than just e
. Thus my code translates the example into
( customers.Where(c => c.City == "London") )
Is the example in the spec wrong, or do I need to be doing processing to recognise whether the enclosing parentheses are necessary? If so, is this in the spec somewhere?
Similarly, 7.16.2.6 (Groupby clauses) says
A query expression of the form
from x in e group v by k
is translated into
( e ) . GroupBy ( x => k , x => v )
except when v is the identifier x, the translation is
( e ) . GroupBy ( x => k )
The example
from c in customers group c.Name by c.Country
is translated into
customers. GroupBy(c => c.Country, c => c.Name)
where again the example result is missing the parentheses suggested by the spec.
e
is evaluated first and select,groupby as applied later. The example uses a setcustomers
where customers is the same as(customers)
, but imagine you have an operator + used for concatenating two lists -from x in a + b
has to be translated into(a+b).Where
. My guess is that () is correct and the example is only simplified version of the same (for this specific case) – Pennywisee
into a different place works like a string insertion, then parentheses are necessary. A more natural assumption would be thate
is a value that can be inserted anywhere.; Does the spec assign different semantics to a parenthesized-expression? In other words, can the expressiona
mean something different than(a)
? I don't think so. That makes it indistinguishable. (As I'm talking about expressions here, the associativity issue does not apply.) – Dumonde
had an addition in it...) I'm not even sure what Roslyn does - maybe I'll try it with an addition and see whether it adds brackets where they're necessary! – Typhoid