Skip to content

Instantly share code, notes, and snippets.

@controlflow
Created November 10, 2016 00:32
Show Gist options
  • Select an option

  • Save controlflow/a9c163f0c1697c23ce76a9b284fa8304 to your computer and use it in GitHub Desktop.

Select an option

Save controlflow/a9c163f0c1697c23ce76a9b284fa8304 to your computer and use it in GitHub Desktop.
switch2.cs
private void RenderCaseLabel(
[NotNull] ICSharpExpression conditionExpression, [NotNull] ISwitchCaseLabel switchCaseLabel,
ref FactoryArgumentsBuilder builder, bool expressionIsPartOfDisjunction)
{
var guardClause = switchCaseLabel.Guard;
var emitParentheses = expressionIsPartOfDisjunction || (guardClause != null && guardClause.Condition != null);
if (emitParentheses) builder.Append('(');
switch (switchCaseLabel.Pattern)
{
case IConstantPattern constantPattern:
builder.Argument(conditionExpression).Append("==").Argument(constantPattern.Expression);
break;
case IDeclarationPattern declarationPattern:
builder.Argument(conditionExpression).Append(" is ").Argument(declarationPattern);
break;
default:
throw new ArgumentException(string.Format("Not supported pattern kind: {0}", pattern.GetType().Name));
}
if (emitParentheses) builder.Append(')');
}
private void RenderCaseLabel(
[NotNull] ICSharpExpression conditionExpression, [NotNull] ISwitchCaseLabel switchCaseLabel,
ref FactoryArgumentsBuilder builder, bool expressionIsPartOfDisjunction)
{
var guardClause = switchCaseLabel.Guard;
var emitParentheses = expressionIsPartOfDisjunction || (guardClause != null && guardClause.Condition != null);
if (emitParentheses) builder.Append('(');
var pattern = switchCaseLabel.Pattern;
var constantPattern = pattern as IConstantPattern;
if (constantPattern != null)
{
builder.Argument(conditionExpression).Append("==").Argument(constantPattern.Expression);
}
else
{
var declarationPattern = pattern as IDeclarationPattern;
if (declarationPattern != null)
{
builder.Argument(conditionExpression).Append(" is ").Argument(declarationPattern);
}
else
{
throw new ArgumentException(string.Format("Not supported pattern kind: {0}", pattern.GetType().Name));
}
}
if (emitParentheses) builder.Append(')');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment