What I’d like in life is a context aware compiler

! Warning: this post hasn't been updated in over three years and so may contain out of date information.

Writing some code today, I realised what I’d really like with type-safe languages is for them to be “type context aware”, which is a term I think I just made up. I’ll explain it with a code example. Given the following interface and class:

I’d then like to be able to write code like this:

In other words, as the if statement tests whether value’s type is B, within the code block following the if statement, value can be taken as read as being of type B. So the compiler should be happy with the statement value.foo(). In reality though, compilers don’t do this. Instead I have to write the highly inelegant code:

So want I want for my birthday this year is for compilers to get type context aware and to handle such code elegantly.

8 thoughts on “What I’d like in life is a context aware compiler

  1. try:

    public function doesSomething(value:A)

    var test:B = value as B
    if (test != null)

    yes ” as ” is slow, and it requires additional locals to type to, but…

    toughts (though I know I’m gonna get them from you anyhow 😉

  2. I’d like to know why you have to ask that question so frequently it matters to you. Why are you passing the wrong type in to this method?

    The joys of simplified examples right? Perhaps this is just an architectural problem that you’re trying to solve by not refactoring?

  3. @Rasheed, you win the “missing the point” prize! 🙂 The example code is not very nice and it is a highly contrived example of the problem. It’s supposed to illustrate the problem though in as few lines of code as possible, not demonstrate good code writing.

    It is an architectural problem though, you are right. Sadly the only way to refactor it in most cases is to not use PureMVC. This tiny change to the compiler would solve one of the worst features of PureMVC, namely all the casting that one must do.

  4. I agree, this would make code much more readable and save time creating new objects just for the purpose of a specialized case.

    Disagree with Rasheed, you are not passing the wrong type into the doesSomething() function, as B is a subtype of A. Cassting is an important feature of OO, if you are required to refactor / rearchitect every time you write a cast you will never ship anything!

Comments are closed.