class constructors in Xtend
Asked Answered
S

4

5

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()
    }

}
Sequestration answered 7/11, 2011 at 8:54 Comment(3)
Did you try def Scope() { } ?Pesek
Ahaa! That works. Now how would I access the superclass constructor? super doesn't seem to work as it usually does. Still new to this.Sequestration
Just a followup, see the comment below for an explanation. It didn't actually work. ;)Sequestration
D
13

Constructors are defined by overloading a new() method:

class MyClass extends AnotherClass {
  new(String s) {
    super(s)
  }

  new() {
    this("default")
  }
}

look here

Drennen answered 7/2, 2012 at 12:51 Comment(0)
M
2

The next release of Xtend is planned for mid December. It'll have support for declaring constructors.

See http://www.eclipse.org/Xtext/xtend/#whatsnext

Masthead answered 14/11, 2011 at 7:44 Comment(0)
P
1

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.

Pontificate answered 8/11, 2011 at 19:29 Comment(0)
M
1

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.

Musca answered 8/11, 2011 at 20:42 Comment(2)
Cheers dude. Definitely a show stopper. This has actually made me look at into scala.Sequestration
This has changed in Xtend 2.1; there are other substantial improvements as well. Still no final variables.Musca

© 2022 - 2024 — McMap. All rights reserved.