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)?

5 Comments

  1. As I wrote in the comment to my blog post, it’s not important if the “: Person” (P) is there in the source, or if the “: Person” (P) is shown as a visual aid by the IDE. There is no difference.

    - if for example P is automatically added by the IDE to the source and newly infered when the call changes its return type

    or

    - if P is just shown, but not added to the source (and infered everytime it’s shown)

    there is no difference from the point of view of the developer. He can’t see a difference when using the IDE. He only sees a difference when going to the source directly.

    There is only a difference when the developers adds a “: Nameable” to the source, which the IDE cannot infer.

    Peace
    -stephan

  2. Nice blog design btw.

    • Jari
    • Posted November 6, 2007 at 10:18 pm
    • Permalink

    Yeah, the difference is fairly small, but it still exists. For example, I find beauty in Ruby code more often than I do in Java. Ruby is concise and easy to read. Java has a lot of extra fluff when compared to Ruby, and while that fluff (types and what-have-you) can nowadays be quickly generated and managed with an IDE, you still have to read it even though you don’t have to write it by hand.

    It’d take a working implementation of the type annotation idea to see whether it would make a difference in practice. Still, my opinion on this is that if a language supports type inference like Scala does, explicit type declarations in physical code are redundant. Of course, the higher the level the IDE operates on, the less the developer has to care about such pesky details as “explicit type declarations” vs. “UI annotations based on inferred types.” :)

    And thanks, the design is just a stock WordPress.com theme called ChaosTheory :)

  3. I’ve been developing Ruby for some years and yes noise free and concise. When you have more than 10 developers on the project and develop for several years problems increase, and Ruby is not so nice any more and not so concise.

    As stated above or in my blog, the IDE could remove most of the Java ‘fluff’ like generics or anotations or public modifiers or ; - and I wish it would.

    I’m playing with Scala. When I’ve done a project with more than 10 people for more than 2 years (better yet 30 people and 5 years) I will have an opinion if explicit type declarations are useful or inference is all you need. I have seen several 30+ / 5+ projects in Java, none yet in Scala :-)

    Peace
    -stephan

    I googled for the theme but haven’t found it :-)

    • Jari
    • Posted November 7, 2007 at 9:04 am
    • Permalink

    The theme was just released but apparently it’s exclusive for WordPress.com users only, which I guess means it’s not available for download if you host your own WP instance.

    And yeah, we’ll see what happens in Scala-land once it catches on and people get more experience in actual development projects. There are things I like about Lift, but some of the stuff seemed a bit ugly to me - but that may have just been the fact that Scala’s syntax is a bit complex in my eyes, especially some of the functional constructs. But that’s a topic for another blog post :)

One Trackback/Pingback

  1. [...] Jari wrote something about types inference, scala and IDE annotations on his blog. Bookmark at:StumbleUpon | Digg | Del.icio.us | Dzone | Newsvine | Spurl | Simpy | [...]

Post a Comment

*
*