|
|
|
|
|
|
Compiler-Checked Strings
A Suggestion for C#
Eric Gunnerson's blog has a discussion of using "compiler information in code", in C#. This includes things like the current file name and line number. There has also been some discussion of a "nameof" operator in C#. I won't reiterate those discussions here, other than to say they're worth reading (and the rest of this page won't make sense if you don't :-)
I was about to post a suggestion to Eric's blog, when I realised that it would be easier to describe it here.
My suggestion relies on the introduction of a new delimiter. Here, I'll use backquotes. I.e. the backward sloping single quote, that looks like this: `
Compiler Checking of Property Names
Imagine some existing data binding code. Something like this:
lbFiles.DisplayMember = "MyProperty";
The compiler cannot check that MyProperty is a valid property name. That could also be an issue for refactoring tools. Ideally, if you change the name of the property (in the class where the property is defined) then a good refactoring tool would automatically change the "MyProperty" string too. However, should the tool change all occurrences of "MyProperty"? No, it can't. Since if "MyProperty" appears several time in your code, the tool can't tell which (if any) refer to the class that you changed.
So, here's my suggestion. Imagine something that looks like this:
lbFiles.DisplayMember = MyClass.`MyProperty`;
The property name is qualified by the name of the class in which it appears. The ` symbols surround only the piece that you actually want to use. So the above sets DisplayMember to "MyProperty", just like the original code did.
In these examples I also change the colour of the backquoted text. That's what the IDE should do if this scheme was used, changing it to a unique colour to make it clear that this is a "quoted identifier", checked by the compiler.
In summary: an identifier appears in the code. It may include a leading class name and even a namespace. You can take a piece of that identifier, and treat is as a string, by "quoting" that piece with ` symbols. The compiler checks that the identifier is valid. The compiler does not care what the identifier represents - it might be a property, a class, a method or a local variable. The compiler only cares that it exists.
Note how this is very similar to the way the compiler deals with "see" tags in XML comments.
Magic Identifiers
This scheme could easily be extended to support "magic" identifiers to represent the name of the current file, the line number etc. E.g.
message = "Created at line " + `#line`+ " of file " + `#file`;
To avoid any possible ambiguity, the "magic" identifiers would have to start with some character, like #, that cannot be the start of a "real" identifier.
Update, 9 Dec 04: I just realised that backquotes make the ideal delimiter, better than the options I suggested before. I've updated the page with backquotes and taken out the other ideas. Can't believe it took me over 6 months to think of this! :-)
Page History: Created 24 Aril 04
Updated 9 Dec 04
|
|
|
Copyright (c) 2003-2006, John Rusk.
|
||