by
Dean Cooper >> Wed, 2 Feb 2000 4:00:27 GMT
Question for JADE developers: Is there any chance that JADE might have this feature in the future? Either overloading methods, or at
least passing parameters to the constructor?
Regarding method overloading. It's absence is not an oversight. Method overloading was considered in the early days of the JADE language and was left out deliberately. There are two schools of thought with respect to overloading, both of which have been represented at different times by different people in the design of the JADE. There are those who think overloading is useful, and those who think it isn't and adds unnecessary complexity and potential for ambiguity. We accept, though, that this is a bit of a religious argument, and there will always be those who disagree. However, at all times, we look to preserve the simplicity, brevity and conceptual economy of the JADE language, and we don't view overloading as necessary. There are more expressive features of higher priority that we would rather invest time in building, whereas overloading, as Meyer puts it, is "no more than a syntactic facility which relieves programmers from having to invent different names for different implementations of an operation". Method overloading is not being considered for any future JADE release at this stage.
Regarding parameters on constructors. We agree that constructor parameters are useful and it would be good to consider them in a future release (probably not 5.1 though). Our current thinking is to allow parameters to be specified in the create method, and add new syntax to the constructor signature to specify which parameters (if any) are to be passed to the superclass constructor (which may have a different number and/or type of parameters). You will still only be able to specify one create method per class though
(allowing multiple constructors a la C++ introduces overloading). One issue with this is that where you dynamically specify the type of an object in a create, the compiler won't be able to check the constructor parameters at compile time; it will have to be a runtime check. For example, in JADE you can write:
createSomething(c : Class) : Object;
vars
o : Object;begin
create o as c;
return o;
end;
Say we had new constructor parameter syntax, for example:
create o("hello", "world") as c;
The compiler cannot validate the constructor parameters because the type of object to be created is determined at runtime by "c" (it's constructor may not even take any parameters!). Languages like C++ don't support this kind of creation and therefore don't have this issue. Some people may object to this as a loss of type safety. These are the kind of issues we need to kick around.
Regarding an example that has been used in a couple of posts:
begin
Transaction;
create p("Oscar");
commitTransaction;
Even if you could pass "Oscar" as a constructor parameter, what do you do in the constructor if the name is null? Or invalid? Or a duplicate of another name? Typically you want to validate the parameters, so you need some way of indicating an error. You could have the constructor raise an exception, but then the example might look like:
on Exception do someExceptionHandler(exception);begin
Transaction;
create p("Oscar");
commitTransaction;
// and you also need the exception handler method
Or you could set a status on the object to indicate an error, and do something like:
begin
Transaction;
create p("Oscar");
if p.status = StatusOK then
commitTransaction;
// return the new object or something
else
abortTransaction;
// return some error
endif;
Or you may have an operation that needs to create Person objects in conjunction with several other related objects, and you may want to set only the attributes on all of the objects before setting the references between them (to avoid unnecessary key maintenance, for example). In this case, you can't do everything in the constructors.
The point here is that these cases have additional issues to deal with beyond just creating the object(s). Many frameworks have initialization methods (eg: initialize, create, etc) that you must call after actually constructing an object; even if you can pass parameters to the constructor. Such methods can then perform secondary initialization, validation, return an error code, whatever. The main issue is encapsulating the operations required to create an object. Typically you wouldn't inline the creation of Person objects everywhere you needed to create them. You'd often want to have a method (eg: createPerson that takes all the required parameters and validates them if necessary) on some owner/parent or factory object that encapsulates the creation of Person instances. In such a design, whether or not you can pass parameters to a constructor is of minimal consequence.
I agree that constructor parameters are a good idea, and it would be good to consider providing them in JADE in the future. But in most systems (certainly for your persistent objects) there is generally much more involved in preparing an object for life than simply executing its constructor.
Dean.