The Ironism

The Ironism

The lair of Lars J. Nilsson. Contains random musings on beer, writing and this thing we call life.

March 2012


More on Constructors

Warning: This post contains fictional grammar, ideas off the cuff and high levels of programmer nerdery. Proceed at your own risk. The whole shebang was started off here: If I Would Design a Language.

Here’s some more you could do with implicit constructors. Remember this class?

It turns out that the constructor argument “name” live on within the scope of the class. That’s interesting, because one of the main pains in Java is beans, and bean accessors. We’ve already seen that we can alleviate that with field access. But how about making it even simpler. Have a look at the following, mutable and completely awesome bean:

Adding the “@Field” annotation generates automatic fields, including getters for the argument, and since he class is mutable, it also generates setters. The above would be equivalent to the following Java bean:

The amount of crud reduced is ridiculous. There is of course the problem that if you mix “@Field” annotations with ordinary arguments and an explicit constructor it might get too messy. But hey, it’s a neat idea.

The proprietor of this blog. Lunchtime poet, former opera singer, computer programmer. But not always in that order. Ask me again tomorrow.

    Comments 0
    There are currently no comments.