[Vala] I'm missing Java's anonymous classes
- From: Jim Peters <jim uazu net>
- To: Vala Mail List <vala-list gnome org>
- Subject: [Vala] I'm missing Java's anonymous classes
- Date: Fri, 5 Aug 2011 18:55:32 -0500
This is an inconvenience rather than a project-killer, but still ...
I know that the Java page suggests that instead of using anonymous
classes, to use delegates, but it just doesn't work out as hoped.
For example (much simplified):
public abstract class Widget : Object {
public abstract void relayout();
}
I want to write this but it's not allowed:
var xx = new Widget() {
public void relayout() { ... }
};
Switching to delegates:
public class Widget {
public delegate void RelayoutDelegate();
public RelayoutDelegate relayout;
public Widget(RelayoutDelegate arg) {
relayout = arg;
}
}
var xx = new Widget(
() => { ... }
);
This seems okay so far, but here comes the killer:
public class Display : Widget {
public Display() {
base(relayout_imp);
}
private void relayout_imp() { ... }
}
This crashes when relayout() is called because the 'relayout_imp'
argument to the constructor needs 'self' but 'self' hasn't been
initialised yet (so is NULL).
Any attempt to work around this makes the whole thing more verbose and
more error-prone -- for example, assigning the delegate after
construction means that the delegate can't be called during
construction -- and the whole thing just gets into a big tangled mess.
So I've gone back to Java style, only I have to create separate dummy
classes now to provide the callbacks.
The code is a lot more readable when the callbacks are inline (either
as delegates or as an anonymous class), but I really can't see a
sensible and safe way of doing this in Vala. (In real life I have
several methods that may need overriding, and several layers in the
object hierarchy).
Any thoughts on this?
Jim
--
Jim Peters (_)/=\~/_(_) jim uazu net
(_) /=\ ~/_ (_)
UazĂș (_) /=\ ~/_ (_) http://
in Peru (_) ____ /=\ ____ ~/_ ____ (_) uazu.net
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]