Tag Archives: compilers

Stephan Schmidt has a blog entry on how explicit static type declarations are for the benefit of the developer, and not the compiler. That is, when you have something like this:

Person person = mysteriousCall()

you know at a glance that the object reference person is in fact of type Person. To a compiler, this might be obvious through type inference - in this case, by looking at the return type of the mysteriousCall function.

In Java, you always declare the member types explicitly, but for a statically typed language that does rely on type inference (I’m thinking Scala), the type declaration Person is clearly redundant. And if there’s one thing any programmer hates, it’s redundancy. So, one could just go with

val person = mysteriousCall()

and be done with it: the compiler will infer the type. Now, Schmidt makes a good point in that now the type of person is not immediately obvious to the naked eye, since there’s no explicit declaration in sight. This gave me an idea, which I posted as a comment to the blog entry:

What if a smart IDE could consult the compiler and decorate / annotate the above code line visually by showing the type of the person field? Now, the type would be explicitly shown to the user, but information that’s available through type inference wouldn’t have to be duplicated in the actual code.

Something like

val person : Person = mysteriousCall()

where : Person is the visual aid shown by the IDE, in standard Scala type declaration notation. Now, the type of the value is clearly visible, but there are no redundant type declarations in the code.

I wonder if anyone’s thought of this before (likely), and whether some tool already implements this for some language with type inference (less likely)?