Groovy/Groovy++ object creation

Groovy‘s as keyword allows for some interesting features. In particular, it provides a convenient syntactic sugar for object creation:

Point point = [4, 2] as Point
KeyAdapter listener = [
        keyTyped: { e -> … },
        keyReleased: { e -> … }
] as KeyAdapter

Still in development, but already quite usable, there is Groovy++: a library which adds static typing, performance and much more to Groovy language. One thing you get addicted to quite soon is that you don’t have to write as in the cases above anymore. If the compiler can statically figure out the expected type, it will insert all the necessary casts for you. This works, for example, for variable declarations, method return types and even method calls. So, if you have

JComponent comp = …

you can then write

if (comp.contains([x:2, y:4])) { … }

Cool, isn’t it? But why am I writing about all this in IntelliJ IDEA blog? Well, just to boast. Unusual syntax is not a reason for not helping you in coding, right? So, now the converted list/map literals will be shown if you search for the constructor usages in IntelliJ IDEA:

And the syntax highlighting on the brackets will help you distinguish between plain list/map literals and the ones that serve for object creation:

Finally, you can easily navigate to the constructor called via a literal. Just place your caret before the opening bracket and go to the declaration (e.g. press Ctrl+B)!

All this will be available in the next IntelliJ IDEA X EAP.

This entry was posted in New Features and tagged , . Bookmark the permalink.

3 Responses to Groovy/Groovy++ object creation

  1. Artem says:

    WOW, good news!
    I think Groovy++ is very promissing project, and it’s very cool that IDEA already support it.

  2. Demeter says:

    Wow! I like that.

    Will IDEA support Groovy++ @Traits as well ?
    http://groovy.dzone.com/articles/how-statically-typed-meta
    http://develosaur.ru/article/мои-эксперименты-с-groovy

    IDEA 9 shows an Method-Not-Implemented Error for any class that implements @Trait.
    @Typed class WebSocketChatController implements org.mbte.grails.languages.ControllerMethods {
    ….
    }

  3. Peter Gromov says:

    Demeter, where is the org.mbte.grails.languages.ControllerMethods trait defined?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>