[Laszlo-dev] Re: [Laszlo-user] Interface support
Oliver Steele
steele at laszlosystems.com
Mon Jun 13 12:05:43 PDT 2005
This is one of the most often-requested features.
Here's what it would take to do it. This is pseudocode, not a patch
file or anything; someone would have to take this and run with it to
make it happen.
Add an entry to lzx.rnc:
interface = element interface {
element method { nameAttribute }
}
Add this line to the definition of toplevelElements in lzx.rnc:
& interface
Allow 'extends' in <class> tags (again, in lzx.rnc):
attribute extends {token}? &
+ attribute implements {string}? &
Add new file org/openlaszlo/compiler/InterfaceDefinition.java:
public class InterfaceDefinition {
private final String mName;
// List<String>
private final List mMethodNames = new Vector();
...constructor...
public void addMethod(String name) {...}
public List getMethods() {return mMethodNames;}
}
Add a field and accessors to ViewSchema.java:
/// Map<String, InterfaceDefinition>
private final Map mInterfaces = new HashMap();
public void addInterface(InterfaceDefinition id) {...}
public InterfaceDefinition getInterface() {...}
Add a new class InterfaceCompiler.java:
public class InterfaceCompiler {
static boolean isElement(Element element) {
return element.tagName().equals("interface");
}
public InterfaceCompiler(CompilationEnvironment env) {super(env);}
public void compile(Element element) {
InterfaceDefinition id = new InterfaceDefinition();
for each (Element method in element.getChildren("method"))
id.addMethod(method.getAttribute("name"));
env.getSchema().addInterface(def);
}
}
Add this line to Compiler.getElementCompiler:
} else if (InterfaceCompiler.isElement(element)) {
return new InterfaceCompiler(env);
And, finally, the tricky part. Something like this in
ClassCompiler.compile():
String implements = element.getAttribute("implements");
if (implements != null) {
Set inheritedMethods = model.getInheritedMethods();
Set undefinedMethods = new HashSet();
for (String interfaceName in implements.tokenize()) {
InterfaceDefinition id = env.getSchema().getInterface
(interfaceName);
if (id == null) {
env.addWarning(...);
continue;
}
for (String methodName in id.getMethodNames())
if (!inheritedMethods.contains(methodName))
undefinedMethods.add(methodName " in " interfaceName)
if (!undefinedMethods.empty())
env.addWarning(name + " doesn't implement " + ListUtils.format
(undefinedMethods)));
}
}
And you need to write ClassModel.getInheritedMethods():
protected Set getInheritedMethods() {
Set methods = new HashSet();
addInheritedMethodsTo(methods);
}
protected void addInheritedMethodsTo(Set methods) {
methods.addAll(self.methods);
if (superClass != null)
superClass.addInheritedMethodsTo(methods);
}
And some test cases for inheriting from one interface and two
interfaces, and the error cases:
- name of the interface isn't valid
- two interfaces have the same names
- <interface> contains a tag that isn't <method>
- <method> doesn't have a @name attribute.
- method/@name isn't valid
- class/@implements isn't valid
This doesn't allow an interface to declare the number of parameters,
or attributes, but that could be an extension later --- this keeps it
simple.
Does this look reasonable?
On Jun 13, 2005, at 11:04 AM, kenly browne wrote:
> hey..good day
>
> I just want to know that is it possible in the future
> that laszlo might support interfaces..
>
> eg.. <interface name="support">
> <method name="laszloDevelopers"/>
> <method name="pleaseImplement"/>
> </interface>
>
> I'm comming from a flex background and that made the
> coding real powerfull.
>
> Thanks for the time
>
> K,Browne
> Developer
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Laszlo-user mailing list
> Laszlo-user at openlaszlo.org
> http://www.openlaszlo.org/mailman/listinfo/laszlo-user
>
More information about the Laszlo-dev
mailing list