class constructors in Xtend
Domanda
I'm trying out Xtend. Is it possible to make constructors? It seems so simple, but I'm getting an error when I try something like this:
class Scope extends Rect {
public Scope(){
super()
}
}
Soluzione
Constructors are defined by overloading a new() method:
class MyClass extends AnotherClass {
new(String s) {
super(s)
}
new() {
this("default")
}
}
look here
Altri suggerimenti
The next release of Xtend is planned for mid December. It'll have support for declaring constructors.
Constructors are not yet supported in Xtend. The suggestion def Scope() is more a bug than a working constructor. You may want to follow this ticket.
There is no constructor support in Xtend 2.0. I think it is a show stopper.
"class Foo { def Foo() { /stuff/ } } declares a method Foo on instances of Foo with the inferred return type, not a constructor, as browsing the generated Java code will show.
This implies that there is no way to extend Java classes that lack default constructors. XTend does not complain; it happily generates Java that does not compile.
Nor does XTend support immutable (final) instance variables, naturally enough.