It is mostly a "chicken and egg" problem. Classes extend the language, so the compiler needs to understand all the classes before it can understand your program. In particular, in OpenLaszlo, classes define new XML tags and the valid attributes those tags can have. The compiler has to operate in two passes: first, find all the classes to determine the XML schema, second parse the program using that schema and compile it. If classes could appear anywhere, the first task would be much more difficult.
A second reason is more computer-science theoretical. You might expect a class that is defined not at the top level to only have a scope of the enclosing context, and to be aware of the enclosing context (this is how inner classes work in Java, for instance). While these features are theoretically possible, we haven't seen the need for them. We think this is a reasonable balance of simplicity and power. Finally, OpenLaszlo has a feature, "Instance-first development" (http://bit.ly/3TdASc), which we think obviates the need for inner or local classes. Because you can add attributes, methods, handlers, events, etc. to instances, there is no need to be able to define a local class to make just one instance: Turning your example on it's head, when you find yourself wanting to write: > <view x="100"> > <class name="foo" width="100" height="100" bgcolor="cyan"/> > <foo/> > </view> You can instead write: > <view x="100"> > <view width="100" height="100" bgcolor="cyan" /> > </view> When you find that you have written the same instance more than once, it is trivial to turn that instance into a "pattern" that can easily be shared so: > <view x="100"> > <view width="100" height="100" bgcolor="cyan" /> > <view width="100" height="100" bgcolor="cyan" /> > </view> becomes: > <class name="pattern" width="100" height="100" bgcolor="cyan" /> > <view x="100"> > <pattern /> > <pattern /> > </view> All you have to do is change the tag to `class` and give it a name (and move it to the top level), and it becomes a pattern. This holds for arbitrarily complex cases: > <sometag> > <handler name="oninit"> > ... > </handler> > <method ...> ... </method> > ... > </sometag> Can become a pattern: > <class name="anothertag" extends="sometag"> > <handler name="oninit"> > ... > </handler> > <method ...> ... </method> > ... > </class> [If your patter extends <view> we let you omit the `extends="view"` for simplicity. If you are extending another tag, you need to specify the `extends` attribute.] Hope that is helpful. Now a question for you: Are there things you would want a local class to do that cannot be done by a top-level class or just extending an instance? On 2009-11-17, at 02:50, Rami Ojares / AMG Oy wrote: > Why is it that classes need to be defined at the top level? > > Example: > > Allowed: > <canvas debug="true"> > <class name="foo" width="100" height="100" bgcolor="cyan"/> > <view x="100"> > <foo/> > </view> > </canvas> > > Not allowed: > <canvas debug="true"> > <view x="100"> > <class name="foo" width="100" height="100" bgcolor="cyan"/> > <foo/> > </view> > </canvas> > > - rami
