Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » QVT-OML » [QVTo] Can a constructor call another constructor?
[QVTo] Can a constructor call another constructor? [message #1849776] Wed, 02 February 2022 12:46 Go to next message
Francesco Bedini is currently offline Francesco BediniFriend
Messages: 32
Registered: March 2018
Member
Is something like this possible?

constructor AClass::AClass() {
	id := "...";
       // Set many other attributes...
}

constructor AClass::AClass(name:String) {
	// call new AClass()   <--
       name := name;
}
Re: [QVTo] Can a constructor call another constructor? [message #1849777 is a reply to message #1849776] Wed, 02 February 2022 13:14 Go to previous messageGo to next message
Christopher Gerking is currently offline Christopher GerkingFriend
Messages: 115
Registered: April 2011
Senior Member
Hi Francesco

I'm afraid this is currently neither part of the QVT specification, nor supported by Eclipse QVTo. I agree that it makes the development of convenience constructors impossible. However alternatively you could define a single constructor with all required parameters, using multiple 'factory' helpers to invoke that consutructor more conveniently.

helper newAClass() :: AClass {
	return newAClass("");
}

helper newAClass(name:String) :: AClass {
	return new AClass(name);
}

constructor AClass::AClass(name:String) {
        id := "...";
        name := name;
}



Kind regards
Christopher
Re: [QVTo] Can a constructor call another constructor? [message #1849779 is a reply to message #1849776] Wed, 02 February 2022 13:23 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

lf you have a default constructor and an application constructor, you may expect an automated call of the default from the application. In a more complicated scenario you might want a cascade of constructors at least some of which would need to pass arguments.

Your default constructor has no arguments, so it is unclear how a non-trivial number of properties would be usefully initialized. For a trivial number of properties duplicating the initialization code is not a great burden especially if you use a helper.

I have never used a constructor preferring the more direct object construct. Faced with the same problem I might go for an inner helper for the basic object creation and wrapping helpers to assign extra properties.

As you indicate, to make cascaded constructors work there would need to be a new constructor call syntax to enable an outer constructor to delegate to an inner constructor. Rather than "call new AClass()" I would suggest that "AClass();" or perhaps "result();" should be adequate.

Please raise a Bugzilla if you feel that this extra syntax sugar is really useful.

Regards

Ed Willink

[Updated on: Wed, 02 February 2022 17:35]

Report message to a moderator

Previous Topic:Can black-box library use metamodel from workspace?
Next Topic:Modeltype metamodel
Goto Forum:
  


Current Time: Thu Mar 28 15:18:10 GMT 2024

Powered by FUDForum. Page generated in 0.04341 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top