Skip to main content

All your property are belong to us

Posted by forax on January 5, 2007 at 8:01 AM PST

Happy new year everyone.


Since my last post, i've done some homeworks :),

and i'm please to present you a new version of the
prototype java compiler which includes property support.

First, why properties ?, we have already fields and any IDE
have a menu item that can generate getters and setters, so why do,
we need properties ?

Why do we write getter and setter ?
The reason, is that when you use
a property, you don't want to know if its implementation use
a field or something else.
By example, you can first choose to implement it as a field
and later when a new feature is require change your code to use a method.
To be binary backward compatible,
a carefull developer will always define a getter and a setter,
even if the property is a field package visible,
but its a boiler plate code and i don't see a reason
why we should do that if the compiler ensure that
a change in the way property is written
will always be backward compatible.

The compiler must handle properties for you and
garantee that implementation change must be binary compatible
without writing boilerplate codes.

Defining a simple property

  public class PropertyPoint {
    property int x;
    property int y;
  }
  ...
  public static void main(String[] args) {
    PropertyPoint p = new PropertyPoint(1, 2);
    System.out.println(p.x + " " + p.y);  // replaced by p.getX(), p.getY()
  }

A word about the keyword 'property', 'property' is not a real keyword
like 'public' or 'enum', so you can have a variable named property
in another place in your code, i will still compile, even if you
create a class property, it will still compile.
Basically, if the compiler found the keyword 'property' in another place,
it consider it as an identifier or a type depending on its location.
This example, written by peter ah

Related Topics >>