Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for passing SyntaxKind to factory methods of class Syntax?

Tags:

c#

roslyn

In Roslyn CTP the following signature of methods is used quite commonly:

LiteralExpressionSyntax LiteralExpression(SyntaxKind kind, SyntaxToken token)

What is the reason for passing SyntaxKind? token itself has Kind property, why Roslyn do not use it?

To be more precise, it takes user-provided kind, asserts that it is limited to a narrow set of values, then checks that token's Kind also belongs to this narrow set, and then passes user-provided kind further.

Why Roslyn not uses token.Kind and makes user to pass explicit kind?

UPD: Actually, the main question is: what are situations when kind, passed by user, will be different than token's kind?

like image 961
Mikhail Brinchuk Avatar asked Mar 23 '23 10:03

Mikhail Brinchuk


1 Answers

If you look closely, you'll see that the kinds checked are not actually the same in each case. There are separate SyntaxKinds for and expressions - so you would have a SyntaxKind.TrueKeyword for the token, and SyntaxKind.TrueLiteralExpression for the resulting LiteralExpressionSyntax. To answer your updated question, the resulting expression.Kind will never match the token.Kind.

However, it does appear to be the case that there is a one-to-one correspondence of token kinds to expression kinds in this case, which means we could derive the expression kind from the token kind. I'll raise this internally.

like image 53
Kevin Pilch Avatar answered Apr 26 '23 20:04

Kevin Pilch