Could JavaFX has the common syntax for object literal, anonymous classes, and AOP?

Well,
I have one idea, I could not say it is stupid or not, but looks amazing.
What the following construct is?

Frame {
    title: "Ch1 Demo"
    width: 300
    height: 250
    visible: true
}

Well it is equivalent of:

x = Frame;
x.title= "Ch1 Demo"
x.width= 300
x.height= 250
x.visible= true

or Java’s equivalent (informal, because javafx.ui.Frame is not directly avalable in Java):

Frame x = new Frame();
x.setTitle("Ch1 Demo");
x.setWidth(300);
// and so on

Of course this is just one of the possible equiv in Java… “Straight forward” I could say…

But I begin to think about alternative equivalents:

Frame x = new Frame() {
   // there is no way to add new constructor or initialization code, but let us imagine there is overloadable init() method called from Frame constructor:
   @Override;
   protected init() {
       setTitle("Ch1 Demo");
       setWidth(300);
   }
   ... 
}

So I propose the following:
Let the object literal construct to allow also method overriding, adding triggers and so on, just for example:
Let’s assume our frame has paint() method (bad example I know — but let assume such method exists). So let it be possible:

Frame {
    title: "Hello, Wolrd!" on replace {
      throw ModificationProhibitedException; // we prohibit further modification of attribute
    }
    width: 300;
    // and so on
    paint() {
      System.out.println("before paint");
      super.paint();
      System.out.println("after paint");
    }
}

Implementation of such feature may be:
1. As I have already introduced via a kind of “anonymous” classes.
2. Via proxifying original class (using mechanism similar to java.lang.reflect.Proxy or LIBGC).

Of course if there is no method overriding or trigger, then object literal will be as simple as object initialization.

OK let’s assume we have variant 2.

Then it would be possible to extend object literal construct to the following.
Say we already have:

 
var frame = Frame {title: "Name"};

if we allow following syntax:

frame { // from small letter -- because we would like the following will be applied on specified variable only:
     title on replace {
         System.out.println("title is changed :) ");
     }
     paint() {
         ... // we could intercept paint() method.
     }
}

thus it would be possible to assign a pretty a lot of attributes with one shot:

frame {
    title: "new title"; // frame.title = "new title"
    width: 200; // frame.width = 200;
}

So varialble{} will resemble PASCAL’s WITH operator🙂 in such usage scenario.
I could evolve this idea even further
we could treat simple block of code in curly brackets as

Void {
    ...
}

because void is nothing this is just a piece of code… — there is no attribute or method to reference.

How all this may be implemented?
Well I think one of the possible way: for each object to have a data record + some “proxy” over it. This proxy will register overriding original methods and triggers… And constructions like frame { … } should be freely casted to original Frame class and may be “frame instanceof Frame” should return true (not like proxies in Java).

What do you think, mates?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: