Random musing for the day: I was thinking about reserved words in programming languages and whether they're really necessary at a lexical level. As you know, most programming languages define in their lexical grammar a set of words that cannot be used anywhere in the language except when explicitly specified in the grammar. For example, VB reserves the word "Object". So you can't just say:

    ' Error: Keyword is not valid as an identifier.
    Sub Object()
    End Sub

Many languages (such as VB) allow you to work around this by providing some sort of lexical escape that suppresses the reserved nature of the word. So you can say in VB:

    Sub [Object]()
    End Sub

Confusingly, many keywords that we've been adding to VB lately haven't been reserved words, to reduce the need to modify people's code when they upgrade. Instead, they've been contextual keywords, that is to say they're only reserved in certain syntatic contexts. For example, From is not a reserved word in VB in the lexical grammar, but if you start an expression with From and then follow it with an identifier, we say "Oh, yes, you're starting a query..." For example:

    Dim From As Integer = 10
    ' OK: Unambiguously the local variable
    Dim x = From + 10
    ' OK: Unambiguously a query
    Dim y = From a In New Integer() {1, 2, 3, 4}

Which leads me to wonder: why bother with lexically reserved words at all? Why not just make all of your keywords contextual? When I started on VB, I guess I just accepted the practice since that's what the language did before I showed up, but now I'm not so sure. Maybe there's some blindingly obvious reason that I'm not seeing (probably there is). I can think of some historical reasons why keywords weren't all contextual:

  1. Maybe it simplified writing a parser in "the old days," or it simplified building a parser generator.
  2. Maybe it was because people were writing code in editors that didn't have syntax coloring. "int int = 5; int = (int)int * (int)int / (int)int" looks pretty nonsensical if you don't have nice coloring to tell you which are the keywords and which aren't.
  3. Maybe there were grammatical problems with doing it? The previous example makes me wonder about whether C could handle it; I'm not an expert on how the C grammar handles the cast operator.

Anyway, it's not extremely relevant at the moment--we're not going to just start unreserving all the keywords in VB--but just something interesting to think about...