[Laszlo-dev] var initialization

Donald Anderson dda at ddanderson.com
Mon Mar 24 20:57:42 PDT 2008


I know the first one works, as I tested that solution.
On the second, do you mean maybe:
    class Bar {
     function bar() { return 42 }
     var alias = bar();
   }
   (new Bar).alias == 42
If so, I haven't tried, but java allows this, I believe.
Worse, this might be allowed:
  class Bar {
   var a = 1;
   var b = a+1;
   var c = b+a;
  }
and the compiler may be required to figure out the ordering.
I *think* in java you are allowed to have forward references
to other fields.  Not tough to deduce an ordering, but more work.

I think we should back up and pool our two ideas so we can avoid
extra any compiler work in the near term:

  class SingletonClass {
    private static var singleton:SingletonClass;
    public static function instance():SingletonClass {
        if (singleton == null) {
           singleton = new SingletonClass();
        }
        return singleton;
     }
    function SingletonClass() { if (singleton != null) throw "Use  
SingletonClass.instance() instead of new"}
  }

usage: var s = SingletonClass.instance();

It's not thread safe, but I don't think we care about that...
Then maybe we can restrict field initializers in some way so
we can guarantee what we have is correct.

On Mar 24, 2008, at 7:38 PM, P T Withington wrote:

> Is a class var allowed to be initialized to an expression that  
> depends on the class being already defined?
>
> Do either of these work in AS3:
>
> class Foo {
>  static var foo = new Foo;
> }
>
> Foo.foo is Foo
>
> ---
>
> class Bar {
>  function bar () { return 42 }
>
>  var alias = bar;
> }
>
> (new Bar).bar == 42
>
> ---
>
> If either of these is legal, we need to change the class -> JS1  
> compiler to emit var initial value computations in a scope where the  
> class is defined.


--

Don Anderson
Java/C/C++, Berkeley DB, systems consultant

voice: 617-547-7881
email: dda at ddanderson.com
www: http://www.ddanderson.com






More information about the Laszlo-dev mailing list