views:

400

answers:

4

The using keyword has three disparate meanings:

  1. type/namespace aliasing
  2. namespace import
  3. syntactic sugar for ensuring Dispose is called

The documentation calls the first two definitions directives (which I'm guessing means they are preprocessing in nature), while the last is a statement.

Regardless of the fact that they are distinguished by their syntaxes, why would the language developers complicate the semantics of the keyword by attaching three different meanings to it? For example, (disclaimer: off the top of my head, there may certainly be better examples) why not add keywords like alias and import? Technical, theoretical, or historical reasons? Keyword quota? ;-)

Contrived sample:

import System.Timers;
alias LiteTimer=System.Threading.Timer;
alias WinForms=System.Windows.Forms;

public class Sample {
  public void Action() {
    var elapsed = false;
    using(var t = new LiteTimer.Timer(_ => elapsed = true) {
      while (!elapsed) CallSomeFinickyApi();
    }
  }
}

"Using" is such a vague word.

+18  A: 

Your question assumes 3 contextual meanings to 1 keyword is more complex than 3 different keywords with different meanings. Some may contest this.

In my years of C# coding, I can't say I've ever been confused as to the meaning of a 'using'; it's always clear from the context.

Judah Himango
Although I've not personally had an issue with 'using' having multiple uses and meanings, I can see how it might be difficult for some.
JYelton
I've never been confused by the multiple meanings, but my tools have. It would be very nice if all the text-handling tools on my system I've installed over the decades could be upgraded to understand the syntax of all the languages I use, but that doesn't seem likely to happen.
Ken
While I appreciate your opinion, you are not answering the question. My question assumes nothing about complexity or confusion. (I don't find the meanings confusing or difficult to discern.) Considering that you start a new language with a blank slate (and a lot of history and theory), my question is why would they decide to consolidate three features into a single keyword?
gWiz
You say your question assumes nothing about complexity, but it does. You said in your post, "why would the language developers complicate the semantics of the keyword", which implies 3 contextual meanings are more complicated than 3 different keywords. That is contested. As Eric Lippert pointed out, "it's a judgment call".
Judah Himango
Sorry, I stand corrected. Complexity wasn't at the core of my curiosity; I see it as a side-effect of their decision but I can see how my wording can imply that I felt complexity was the central issue. Thanks again for your reply.
gWiz
+7  A: 

I think that a language designer is forced to use as few keywords as possible, in order to minimize name clashes with identifiers. If you imagine someone porting code from C++ or Java, fewer keywords imply a smaller likelihood of clashes.

This is not really in the scope of your question, but this issue is especially relevant when creating a new version of an existing language. C# 3 added contextual keywords, that is identifiers that become keywords only when used in a specific situation. Just to prevent existing code from becoming invalid.

And I agree with @Judas that the 3 usages of 'using' are not confusing (IMHO). C# has already been - wrongly I think - accused of being too much like Java. Imagine what some people would have said if it had an 'import' keyword...

Timores
Very interesting answer. Thank you. (And I agree they are not confusing; that is not the basis of my question.)
gWiz
+6  A: 

Eric Lippert (who has and I believe currently works on the C# team at Microsoft) posted a comment in reply to a similar question on his blog (first comment). Quoting him:

This is a tricky point of language design; when one keyword is used to represent two completely different concepts, it can be confusing. But introducing a new keyword per concept makes the language feel a bit bloated. I personally would have chosen "imports" or some such syntax for the directive form to ensure that it is not confused with the statement form, but I understand that its a judgment call.

Timothy Carter
Excellent meta-answer! Thank you!
gWiz
Yep, I'm still here.
Eric Lippert
+15  A: 

It's a jugdment call. I personally would have made the choice you suggest: use something like "alias" and "import". I note that "alias" is a contextual keyword of C# and is used to declare that an extern alias is in usage in a particular file; it seems more natural to use that for declaring a type alias instead of "using".

The statement form of "using" was actually added rather late in the game; the designers wished to use "using" not just because it is already a keyword, but also because it emphasizes that a resource is being used in a particular region of code and is then going away. The fact that "using" already had a meaning in the directive sense was a happy accident.

If this topic interests you, I've written several articles on it. Here for instance I have articles on how "fixed", "partial" and "into" also have multiple meanings in C#:

http://blogs.msdn.com/ericlippert/archive/tags/What_2700_s+The+Difference_3F00_/default.aspx

Another answer also links to my article discussing how we ensure that not too many words are reserved for use by the language:

http://blogs.msdn.com/ericlippert/archive/2009/05/11/reserved-and-contextual-keywords.aspx

Eric Lippert
This is the answer I was looking for. Thank you! I agree that "using" seems most apt; hence in my example I renamed the other usages. Thanks for links, I look forward to reading them!
gWiz