Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363017

Summary: [Xtend] support for definition of constructors
Product: [Modeling] TMF Reporter: Sven Efftinge <sven.efftinge>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: eclipse.dserodio, jan, sebastian.zarnekow
Version: 2.1.0Flags: sven.efftinge: juno+
Target Milestone: M4   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Sven Efftinge CLA 2011-11-07 02:32:02 EST

    
Comment 1 Sven Efftinge CLA 2011-11-12 06:55:27 EST
The semantics will be just like in Java, the syntax is almost the same.
The single difference is that instead of repetition of the class' name you only need to write the keyword 'constructor'. This reduces redundancy and at the same time gives a nice visual anchor in the text file.

Example:

class MyClass extends AnotherClass {

  constructor () {
    this("MyClass")
  }

  constructor (String name) {
    super(name)
  }
}
Comment 2 Jan Koehnlein CLA 2011-11-13 08:37:07 EST
In Xbase everything is an expression but might compile to a sequence of Java statements. Java does only allow expressions that don't call non-static methods in the constructor's arguments. 

We have to specify/verify what kind of Xbase expressions are allowed as arguments in Xtend's super/this calls.
Comment 3 Sven Efftinge CLA 2011-11-13 11:00:02 EST
My idea was to allow all the expressions and as long as we can't turn it into a Java expression, we generate something like:

super(new Function0<String>() {
    public String apply() {
       // generated Java statements.
    }
}.apply());

In addition we should add a compiler check for this context so invalid calls get marked.

Btw.: we should do the same thing for field initializers.
Comment 4 Sebastian Zarnekow CLA 2011-11-18 10:41:58 EST
To be done:

Validation for super(..) call / this(..) call
Cyclic constructor invocation
Expressify the arguments that are passed to super(..) / this(..)
Compiler

Current syntax proposal:

class Z extends Something {

  new(int a, int b) { 
    super(a + b) 
  }

}
Comment 5 Sebastian Zarnekow CLA 2011-11-21 16:21:35 EST
Pushed working version. Remaining issues will be tracked in own tickets.
Comment 6 Karsten Thoms CLA 2017-09-19 16:58:05 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 7 Karsten Thoms CLA 2017-09-19 17:09:17 EDT
Closing all bugs that were set to RESOLVED before Neon.0