Replies: 5 comments
-
Should maybe be noted: adding one more |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr Right, that turns the expression into an interpolated verbatim string literal, where newlines are allowed. |
Beta Was this translation helpful? Give feedback.
-
Tagging @gafter |
Beta Was this translation helpful? Give feedback.
-
Also consider allowing: IEnumerable <...> list = ...;
string s = $"This is my enumeration: {string.Join(", ", from x in list
where x.SomeCondition
select Process(x))}"; |
Beta Was this translation helpful? Give feedback.
-
@svick My reading of the spec agrees with yours. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When looking at the spec for interpolated string literals because of dotnet/roslyn#26814, I found some discrepancies: as far as I can tell, the lexical grammar says that in some specific cases, newlines should be allowed within interpolated regular (i.e. non-verbatim) string literals.
Specifically, those cases are when the
single_regular_balanced_text_character
rule is not used and instead one of the rulesdelimited_comment
,string_literal
orinterpolated_string_literal
is used.I think the intention is that newlines should not be allowed in those cases and the compiler does not allow them.
Consider this expression:
I believe the lexical grammar in the spec says this should be parsed as:
interpolated_string_literal
'$' interpolated_regular_string_literal
'$' interpolated_regular_string_start interpolated_regular_string_literal_body interpolated_regular_string_end
'$' '"' interpolated_regular_string_character* '{' interpolated_regular_string_literal_body interpolated_regular_string_end
'$' '"' '{' interpolated_regular_string_literal_body interpolated_regular_string_end
'$' '"' '{' regular_balanced_text interpolated_regular_string_end
'$' '"' '{' regular_balanced_text_part+ interpolated_regular_string_end
'$' '"' '{' regular_balanced_text_part interpolated_regular_string_end
'$' '"' '{' string_literal interpolated_regular_string_end
'$' '"' '{' verbatim_string_literal interpolated_regular_string_end
'$' '"' '{' '@"' verbatim_string_literal_character* '"' interpolated_regular_string_end
'$' '"' '{' '@"' verbatim_string_literal_character '"' interpolated_regular_string_end
'$' '"' '{' '@"' '<Line feed character (U+000A)>' '"' interpolated_regular_string_end
'$' '"' '{' '@"' '<Line feed character (U+000A)>' '"' interpolation_format? '}' interpolated_regular_string_characters_after_brace? '"'
'$' '"' '{' '@"' '<Line feed character (U+000A)>' '"' '}' interpolated_regular_string_characters_after_brace? '"'
'$' '"' '{' '@"' '<Line feed character (U+000A)>' '"' '}' '"'
Thus, according to my reading of the spec, this expression should be valid, even though it goes against the presumed intention of interpolated regular string literals and even though the compiler does not allow it.
Is this really a bug in the spec? If not, where am I wrong?
Beta Was this translation helpful? Give feedback.
All reactions