Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: \$ character escape for names #838

Closed
alrz opened this issue Aug 22, 2017 · 11 comments
Closed

Discussion: \$ character escape for names #838

alrz opened this issue Aug 22, 2017 · 11 comments

Comments

@alrz
Copy link
Member

alrz commented Aug 22, 2017

Currently, when you want to build up a string from multiple identifier names you'd end up with this:

var str = $"logged: {nameof(Class)}.{nameof(Class.Member)}";

Which is a lot less readable compared to the equivalent raw string `"logged: Class.Member".

I want to suggest to use \$ and \$@ character escapes (currently an error) for that,

string a = "logged: \$Class.Member";                 // "logged: Member"
string b = "logged: \$@Class.Member";                // "logged: Class.Member"
string c = "logged: \$@Namespace.Class.Member";      // "logged: Namespace.Class.Member"

This should work for non-verbatim string interpolated strings as well.

Note: If you want to use a raw string after . you would have to escape it i.e. \..

Rel: #522

@iam3yal
Copy link
Contributor

iam3yal commented Aug 22, 2017

@alrz Will it have the added benefit to produce a constant as opposed to string.Format?

@yaakov-h
Copy link
Member

How do you finish this type of interpolation? Where's the end boundary?

e.g.:

string a = $"Log {nameof(Foo)}Thing"; // "Log FooThing"
string b = "Log \$FooThing" // ??

Also related: #701, dotnet/roslyn#10972

@iam3yal
Copy link
Contributor

iam3yal commented Aug 22, 2017

@yaakov-h Well, it's only a problem if you have a FooThing, so How about something like this \$Foo$?

Actually, if you have both Foo and FooThing then the result is going to be the same in this case; otherwise, the compiler can pick the type that fits/exists, probably tricky but then yeah my suggestion might make sense.

@alrz
Copy link
Member Author

alrz commented Aug 22, 2017

@eyalsk A string literal is a constant already and this addition would not change that. Note: there are other proposals to make string interolated strings a constant if all of interpolations are constant and there is no format specifier which is unrelated here.

@orthoxerox
Copy link

This will make Visual Studio's refactoring job much harder. The benefit of nameof is that it is a separate syntax node.

@alrz
Copy link
Member Author

alrz commented Aug 22, 2017

@orthoxerox how about another syntax node just like interpolations?

@iam3yal
Copy link
Contributor

iam3yal commented Aug 22, 2017

@alrz I know that a string literal is a constant, not sure whether I was clear but I'm asking whether this version of interpolated string would end up being a constant as opposed to a string.Format call.

var str = $"logged: {nameof(Class)}.{nameof(Class.Member)}";

Same as the following:

var str = string.Format("logged: {0}.{1}", nameof(Class), nameof(Class.Member));

Now, I'm asking whether your version would have the added benefit to be the following:

var str = "logged: Class.Member";

Or is it just the same and the only thing it adds is "readability"?

@alrz
Copy link
Member Author

alrz commented Aug 22, 2017

@eyalsk The point is to keep identifiers in sync in case of renaming etc. But the final string is the same.

@iam3yal
Copy link
Contributor

iam3yal commented Aug 22, 2017

@alrz Ah okay, thanks. :)

@Thaina
Copy link

Thaina commented Aug 24, 2017

More like you need fullnameof(Class.Member)

@alrz
Copy link
Member Author

alrz commented Aug 25, 2017

@Thaina It does not address verbosity caused by nameof+string interpolation mentioned in the OP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants