Closed Eilon closed 9 years ago
@NTaylorMullen let us verify out var in a functions block, and ?. @Foo.FirstOrDefault()?.Something
?.
breaks rendering.
please be more specific about the exact way you're using ?.
and what gets broken. also does the breakage vary depending on
?.
e.g. (foo as Bar)?.Beez
versus foo?.bar
??.
or ?[
e.g. foo?.bar
versus foo?[bar]
or foo?[42]
?@DateTime?.Now
Generates:
Write(DateTime);
WriteLiteral("?.Now");
When it should generate:
Write(DateTime?.Now);
Also need to support exception filters:
Today with this code:
try
{
throw new InvalidOperationException();
}
catch (Exception e) if (e.Message.Contains("Operation is not valid"))
{
}
You get this error
CompilationFailedException: Compilation for 'c:\users\mattgal\documents\visual studio 2015\Projects\WebApplication1\src\WebApplication1\Views\Home\Index.cshtml' failed:
Expected a "{" but found a "if". Block statements must be enclosed in "{" and "}". You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed:
@if(isLoggedIn)
<p>Hello, @user</p>
Instead, wrap the contents of the block in "{}":
@if(isLoggedIn) {
<p>Hello, @user</p>
}
From TFS bug 1099131.
I investigated and it looks like the only pieces that aren't working are:
/cc @DamianEdwards Do you feel strongly one way or the other about exception filters making it?
Just making sure these are tested:
_Using Static Members_
@using ...
(or maybe not)using static System.Console;
_Expression-bodied members_
>
where we might not expect it (or maybe not)public double Dist => Sqrt(X * X + Y * Y);
_String interpolation_
$"{p.Name} is {p.Age} years old."
var @class = "History";
var happiness = 6;
$"I like taking {@class} class? {(happiness > 5).ToString()}."
_Null Conditional Operators_
Breaks by using them in implicit expressions such as @Foo?.Bar
, @Foo?[0]
.
Fixed: 58c0a36200727b449996242ec9976f8f9fd1e612
_Using Static Members_
Breaks in the scenario @Eilon mentioned: @using static System.Console
Fixed: 1879ac642754b5f84e6055580e6fc60e13fa2100
_String Interpolation_ Breaks in the following scenario.
@{
$"abc{"123\""}.def";
}
Note the \"
in combination with the {"
from the string interpolation cause the chaos and results in the }
being treated as the end of the @{
block.
https://github.com/aspnet/Razor/issues/401
_Exception Filters_ Breaks by using them :trollface:
https://github.com/aspnet/Razor/issues/402
I'm addressing each of these issues (in order mentioned) and associating their fixes with this issue. If needed I can break them into separate issues.
What about the following features within a c# block?
nameof
operator (probably works, just confirming)await
in catch
and finally
blocks (probably work and very minor since try
is unusual in Razor source)@dougbu Yup, all of those work :smile:
Investigated string interpolation and the fix for it is relatively complex. Talked to @DamianEdwards about prioritization and it's not a must-have for beta5. Will file two separate issues for exception filters and string interpolation so I can close this issue out in regards to null conditional operators and static usings once I've completed them.
58c0a36200727b449996242ec9976f8f9fd1e612 1879ac642754b5f84e6055580e6fc60e13fa2100
Please don't close this bug until the C#6 features are finalized (which won't be any time soon). We can use this bug to track potential changes we might need to make to Razor. E.g. imagine a new C# 6 feature has some additional
@
signs in it somewhere - we'll likely have to adapt to that.