Max Blog

My Blog

Archive for November, 2009

You can run and you can hide

First of all let me introduce you the “Compilation Unit”. Compilation unit is the smallest code entity that can be compiled properly (to SWF file).

In ActionScript3 those entities are “as” or “mxml” files. (I skip “mxml” because it’s own topic )

The “as” files must be structured as following to become a compilation unit:

–Package declaration
— Class | Interface | Function | Variable | Constant | Namespace declaration
—- (if Interface) Method | Getter | Setter declaration
—- (if Class) Method | Getter | Setter | Constructor | Field | Constant | Namespace declaration
-Local Class | Interface | Function | Variable | Constant | Namespace declarations

So you need a package declaration with an element inside to make it a compilable entity

?View Code ACTIONSCRIPT
package {
  var a;
}

And you can hide declarations from anybody when you put them outside of package declaration

?View Code ACTIONSCRIPT
package {
  public const KingOfPop : Michael = new Michael();
}
class Michael{
  public function dance() : String {
    return "Moon Walk";
  }
}

By the way this is the one and only good implementation of Singleton Pattern in AS3.

But it’s not what I want to show here. What I want to show is that my first definition of compilation unit was wrong because you can write statements between listed declaration and those will be executed as static initializer code.

So check this out:

?View Code ACTIONSCRIPT
package{
  trace("A");
  import flash.display.Sprite;
  trace("B");
  class Test extends Sprite{
    trace("C");
    public function Test(){
       trace("D");
    }
    trace("E");
  }
  trace("F")
}
trace("G")

Can you tell what will be traced? :)

Last but not least conclusion – with this info in mind you can use AS3 as a “real” script language:

?View Code ACTIONSCRIPT
package{ import flash.display; class A extends Sprite}
var a = 10;
var b = 20;
trace("a+b="+(a+b));
posted by admin in AS3 Creative point of view and have Comments (2)

Share your property

Classes in AS3 do have Properties but with some limitations. This code will compile fine:

?View Code ACTIONSCRIPT
package {
  public class ClassA {
 
    var _max : String;
 
    public function get max() : String {
      return _max;
    }
 
    private function set max(max : String) : void {
      _max = max;
    }
  }
}

But this won’t:

?View Code ACTIONSCRIPT
package {
  public class ClassA {
 
    var _max : String;
 
    public function ClassA() {
      trace('max: ' + (max));
    }
 
    public function get max() : String {
      return _max;
    }
 
    private function set max(max : String) : void {
      _max = max;
    }
  }
}

Looks like if you mix property visibilities you can’t refer to it any more.

posted by admin in AS3 Creative point of view and have No Comments

Don’t lose your keys

Every programming language has keywords and AS3 has lots of those. They are even categorized in three groups. Have a look at my favorite website but now under the topic: Keywords and reserved words

You see three categories:

  1. first says – don’t you dare to use me in your code in different context as I meant to be
  2. second says – wellll, I am a keyword but only in some context, so do your best
  3. third says – I am not a keyword, but I could be – don’t you touch me

So, our conclusions – this code is ugly but correct:

?View Code ACTIONSCRIPT
function get get():int {
    return 0;
}
var namespace : Namespace;

Another logical conclusion from the first group is that packages should not be named as keywords. Because if you have a class in such a package than a keyword will appear in your code as identifier in an import statement. Sadly logic is relative from creative point of view. You can name a sub package “default”:

?View Code ACTIONSCRIPT
import my.default.MyClass;

But on the over hand this won’t compile:

?View Code ACTIONSCRIPT
import default.MyClass;

:)

posted by admin in AS3 Creative point of view and have No Comments

AS3 and the kids

AS3 is a proud holder of three inline languages:

  • XML
  • E4X
  • RegEx

I posted a references to usage of E4X in previous post. And here are two reference for usage of RegEx:

http://donttrustthisguy.com/2008/02/29/utilizing-regular-expressions-in-as3/

http://gskinner.com/RegExr/

But what I would really like to show you in this post is some syntax wonders of inline XML. Have a look at this code:

?View Code ACTIONSCRIPT
<root/*an AS3 comment*/>
    <!-- XML comment--> some text
</{(trace("let's close this tag"), "root")}>

It is compilable code. And yes you can use AS3 comments and XML comments inside of inline XML.
You can also embed inline AS3 inside of inline XML and it will be evaluated at runtime. So the close tag is correct in this example.

I hope the parenthesis syntax is familiar from the previous post.

posted by admin in AS3 Creative point of view and have No Comments

Parenthesis – the round mystery

Parenthesis has a really interesting field of application in AS3.

Have a look at this page: Parentheses

The interesting one is to evaluate a series of expressions:

?View Code ACTIONSCRIPT
var a:int = 2;
var b:int = 3;
trace((a++, b++, a+b)); // 7

Very cool stuff it looks like an inlined function. Normally I would write something less creative like this

?View Code ACTIONSCRIPT
function foo(a:int, b:int):int{
    a++;
    b++;
    return a+b; 
}
trace(foo(2,3)); // 7

I also would say that second version is more readable and reusable, but not as cool as the first one.

Another interesting thing about this notation is that you can’t declare variables inside parenthesis, but you can declare anonymous functions:

?View Code ACTIONSCRIPT
var f:Function;
trace((f = function():int{return 7;}, f())); // 7

And that’s not all folks. There is another context where parenthesis have a pretty cool role  - E4X. In E4X you use parenthesis for “filters” declaration.

There are a few nice posts about E4X filters on this blog: http://joshblog.net/2007/05/08/methods-to-filter-data-with-e4x-in-flash-9/

posted by admin in AS3 Creative point of view and have No Comments

ActionScript 3 creative point of view

This will be a sarcastic run of posts about AS3 syntax and language features.

I must say my last big ActionScript project was a few years ago and now I am not an ActionScript developer anymore. I learned AS3 from the Tool Developer point of view, but neither the less I was testing it for some small efforts (BehaveAS).

As tool developer you learn some crazy stuff that no “using” developer would know. This crazy stuff will be posted here, maybe somebody will find it interesting.

posted by admin in AS3 Creative point of view and have No Comments

IDE what makes the difference

An IDE is not a Text Editor. It is a complex piece of software that helps you develop your projects faster.
In my opinion, static analysis is the heart of this software.
When you look at it closer most of the modern compiler also make static analysis on the code before they turn it to byte code.
This gives you a possibility to connect to the compiler and just visualize found errors in the editor.
But now you are facing some requirements for compiler. The compiler has to provide an interface for error collection, it has to be able to compile only the file you are working on, and it has to be very fast if you want to show errors live when the user is typing.
Eclipse JDT for example provides its own modified compiler for this purpose.

But still we covered only one problem, the error highlighting.

What’s about auto completion? For auto completion you also have to do static analysis to identify the scope the auto completion was triggered in and the possible proposals in this context. The static analysis for auto completion is much complex than by error highlighting. The user started to type something and now when his code is not complete he wants to auto complete it. The code is not complete so it won’t compile. I know only one compiler that is able to give auto completion suggestions (haXe).

A good IDE provide you also with ability to navigate/search/format and visualize your code.

Code generation is the next big feature that every IDE have to provide and it also have to be based on static analysis, because code generation should not break code. The generated code should be close to programmers intention. The main goal is you don’t have to modify generated code it should be formatted as you expected it should be placed where you expect it, you are ready to go to extend the generated code.

Refactoring is the key feature of every enhanced IDE and it’s also based on the static analysis because it is the same principle as by code generation: don’t break code, refactor it. Refactoring is also much complicated than just code generation. Because in refactoring you delete existing code and generate a new one. So the danger of breaking code is bigger.

Let’s discuss the second part of a good IDE, simplicity and management.
Creation of projects should be simple. People should be able to make shortcut’s for work flows they do frequently.
Frameworks and other technologies that are used by developers should be supported and seamless integrated in the IDE.
It should be easy to launch the developed application so you can have a look at result of your work at any stage.

Tags:
posted by admin in IDE and have No Comments