Archive for October 2, 2006

Grey Line

In Java, if you’ve created an object from a particular class, you can use only properties and methods that were defined in this class. For example, if the following class:

class Person {
String name;
}

you can only manipulate with the name property:

Person p = new Person();
p.name = “Joe”;
System.out.println(p.name);

ActionScript calls such classes sealed, but it also has different animals: dynamic classes, which allow you to programmatically add new properties and behavior to classes during the run-time. Just add the magic keyword dynamic to the class definition:

dynamic class Person {
var name:String;
}

Now let’s add dynamically two variables name and age and the function printme() to the object of type Person:

Person p= new Person();
p.name=”Joe”;
p.age=25;
p.printMe = function () {
trace (p.name, p.age);
}
p.printMe(); // Joe 25

You do not have complete freedom though: you can dynamically add only public properties and methods. Of course, nothing comes for free and sealed classes are a bit more efficient in terms of memory consumption, because they do not need to create a hash table to store the properties and methods that are unknown during compilation. Another obvious restriction is that dynamically added functions can’t access private members of the dynamic class. Read the article Programming In Style or an Elevator Pitch to see how by just declaring standard Flex component dynamic, the your code becomes more simple and elegant.

In AS3, any function can be attached to a dynamically created property of a dynamic object, for example

function calcTax():Number {…}

var myObject:SomeObject = new SomeObject();
myObject.tax=calcTax; //add the tax property and attach the function calcTax()
var myTax=myObject.tax();

The delete operator destroys the property of an object and makes it eligible for garbage collection:

delete calcTax();
myTax=myObject.tax() // generates an error

Some of the Flex classes were defined as dynamic, i.e. Object, Array, MovieClip, NetConnection, TextField, and others. At the time of this writing, subclasses of dynamic classes are not dynamic by default.
Because of this, you may run into an ugly run-time error: imagine a sealed class S that extends a dynamic class D. If you create an object as
D myObj = new S(), an attempt to add a propery to myObj will produce a runtime error because the variable myObj points at a sealed object.

Let’s do a quick test. Create a new project in FlexBuilder and select ActionScript project as its type. Enter AS_Only_Project as the project name. In a couple of seconds you’ll see the auto-generated code that looks as follows:

package {
import flash.display.Sprite;

public class AS_Only_Project extends Sprite
{
public function AS_Only_Project()
{
}
}
}

Next, create a new class called D and check odd the Dynamic checkbox in FlexBuilder pop-up. You’ll get this class.

package {
public dynamic class D
{
}
}

Now, instantiate and test the dynamic nature of the class D by adding the constructor
public function AS_Only_Project()
{
var myD:D=new D();
myD.favoriteBand=”Pink Floyd”;
trace(“Favorite Band=”+myD.favoriteBand);
}
Run this application in the debug mode, and sure enough it’ll print

Favorite Band=Pink Floyd

Create one more sealed class called S inherited from the dynamic D:
package {
public class S extends D
{
}
}

An attempt to add properties on the fly to the instance of the class S fails miserably as expected:

var myS:D = new S();
myS.favoriteSinger=”Alla Pugacheva”;
trace(“Favorite Singer=”+myS.favoriteSinger);

ReferenceError: Error #1056: Cannot create property favoriteSinger on S.
at AS_Only_Project$iinit()[C:\TheRIABook\eclipse\AS_Only_Project\AS_Only_Project.as:13]

If you’ll try to instantiate your sealed class as follows:

var myS:D = new S() as D;

I have two news for you: the good news is that it compiles, and the bad (and expected) news is that it generates exactly the same runtime error.

Most likely Adobe’s gonna hire a hitman and kill me after the following statement, but I’m going to say it anyway (at least you’ll now who to blame)… May be I should not?…Life is so good, and I’d like to witness the success of Apollo…I’m sayyyiiiinng this:

If you need to add new functionality to one of the existing standard Flex components (buttons, comboboxes and the like), do not bother extending them and creating new classes. Just create one simple empty subclass with the keyword dynamic and instantiate and add new properties on the fly as needed, as was shown by the Smalltalk student in this article.

A sound of a silenced pistol shot. Curtain.

Yakov Fain

Comments (4)

 

Grey Line

Your old programming habits influence the style of your programming in any new language you learn. Let me illustrate my point using Flex.

We usually run Flex training for private clients of Farata Systems , but once in a while we teach public classes for people with different programming background (my next Flex class at New York University starts in November). All students usually get excited by Adobe Flex, but each of them comes with different understanding of how to do things right. So I’ll tell you the story that might have happened in a real life, but first, let me remind you of an old Indian tale about seven blind men and an elephant . In short, one blind man touched the elephant’s head, the other one touched the tail, someone was by the leg. And each of them visualized an elephant differently based on what he touched…

My students usually arrive to the classroom early, but this time three seats were empty. Five minutes later I got a phone call from one of them explaining that they got stuck in the elevator and will stay there for another fifteen minute until the serviceman arrives. Needless to say that each of them had a laptop (do not leave home without it), so I gave them a short assignment to trying to help them use this time productively.

Here’s the assignment: Create a Window with a Panel that can resize itself by clicking on the button +/- that is located in the right corner of the panel. One click should minimize the panel’s height to 20 pixels, and a subsequent one should maximize to 100 pixels, and so on.

Read more…

Comments off

 

Grey Line

Well, not quite, but (hopefully) we are getting  closer.

I have been following closely on the progress of Apollo and whatever news I could find on inroads it makes in different types of applications. It seems that more people who are lucky to get their hands on it are switching from Web applications to disconnected or partially disconnected ones ( http://www.onflex.org/ted/2006/09/svn-client-with-flashnetsocket-in.php is a good example). In other words, we are moving to desktop applications again – this time desktop 2.0. As history repeats itself, let us recap.

Desktop 1 arrived as intelligent and jazzy replacement of mainframe terminal. Terminal emulation has been major function within the first 10 years of Desktop 1 era. Few “killer apps” (1-2-3 and WordStar) changed the way people see computers. You could own your private data. And on the way, new set of technologies named client-server emerged – reflecting relationship between the new and the old. The main selling point of that was the ease of opening up server data to the end-user. And it did – taking away terminals and mainframe UI along the way

The server world fought back – we had 10 years of JEE and semi-intelligent browser terminals

We are getting really close to “client-web” revolution – this time making all the data in the world available and personal in the same time. The trick is to remake all killer applications of the past in a different context. In case of word processor or spreadsheet for me collaboration and privacy are more important then spellchecker (and it probably shows).

Here is what I think the game is going to be. Making application development simplier will get us half way there. From a personal experience, I believe that a small team of developers can create any “Office” component in Apollo before it ships. The other half is to put end-user in charge of “experience” they want. Here is an example - a lot of browser handicaps were “fixed” by toolbars and components. Making RIA platform and applications capable of cross-integration is going to be vital.

There is no question in my mind that working on AJAX and such is just a waste of time. Actually, I personally gave up on AJAX about 2 years ago (just before it got “named”). Microsoft was contacting software vendors who developed browser enhancements to see what features of their product can be used in IE7. We offered bunch of low-level technologies to cure JavaScript/browser problems - reliability, performance and productivity. The answer was that Microsoft is looking beyond the browser as the next Web platform.

This month we will finally start releasing commercial products for RIA platform. I can only assume from what I see and hear that quite a few others are on the verge of product delivery.  If you have interest in making Flex/Flash/Apollo platform succeed, you better get cranking on the next killer desktop app.

PS. I am starting small pet project on Flex virtualization. Quite often we need preconfigured server/application (not just Tomcat, but mySQL, certain messaging, workflow, etc products) to be available to the clients or people going through training here at the company. I was thinking of VMWare nano-vm appliance that could be dropped on any desktop or server for instant deployment. If you have similar requirements and would like to participate, please drop me a line – we can always use good developers and beta testers.

Sincerely

Anatole

 

Comments (4)