Skip to main content



      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 07:46 Go to next message
Eclipse UserFriend
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 08:14 Go to previous messageGo to next message
Eclipse UserFriend
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 08:23 Go to previous message
Eclipse UserFriend
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 12:35] by Moderator

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


Current Time: Thu Jun 19 08:00:55 EDT 2025

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

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

Back to the top