11 March 2011

SELF parameter in user-defined constructor: ambiguity in quiz? (2085)

In the 10 March quiz on user-defined constructor functions, we scored as incorrect the following statement:

"The constructor must pass SELF as one of its parameters."

Several players felt that was ambiguous. Here is a typical comment:

I wasn't sure whether this should be marked correct or not, because the documentation says "A user-defined constructor has an implicit first parameter SELF. Specifying this parameter in the declaration of a user-defined constructor is optional.". Therefore, the constructor function *will always* pass SELF as one of its parameters, whether you explicitly declare it in the code or not. In this sense, the constructor function "must" pass SELF as one of its parameters - but in another sense, the programmer doesn't actually have to declare it explicitly.

While I can definitely see these players' point, the question clearly and explicitly refers to user-defined constructors:

Which of the following statements regarding user-defined constructor functions of object types are correct?

A user-defined function is a function that a developer writes. So when I state in a choice "the constructor must pass," I feel that it is valid to consider that equivalent to writing:

"The constructor function written by the developer must pass SELF as one of its parameters."

It is not true that the parameter list of the developer-written function must include SELF explicitly.

Finally, another player wrote with the following concern:

I have a doubt about wording of the "The constructor must pass SELF as one of its parameters." choice. It seems to me that a function (and a constructor is a function) does not pass its parameters, it accepts (or awaits) them. A calling code passes parameters to the function.

I suppose my verbiage is a little bit "loose," but I believe the intention and meaning was clear.

I do not believe that any re-scoring is required for this quiz.

Back to PL/SQL Challenge version 2 coding and testing!



  1. Yes, it does not deserves rescoring but wording anyway should be changed to
    "The constructor must declare SELF as one of its parameters."

  2. Hello All,
    Unfortunately, seems that the same issue was misleading for many of us.

    What is strange about these constructors is something that maybe the documentation should have explained: Why at all do they have a parameter defined (SELF IN OUT) to which
    in fact you CANNOT pass a value when calling the function ?
    I suppose that it is related to some internal implementation, when the constructor is called internally, the caller DOES PASS it a value for this parameter (though SELF it is also the
    return value of the function).

    So, from this aspect also probably our ambiguity arose related to this quiz.

    Also, while performing my tests, it happened to me to forget at all the RETURN; statement at the end of the constructor, and it DID NOT work. The compilation was successful, but at run time the same error happens as for a regular PL/SQL function that does not have a "RETURN value" coded.
    If so, then why in fact it doesn't accept
    for example "RETURN SELF" ?

    ... Many questions still ... these object-oriented features are "not yet matured enough" in our "under-conscience" to have them "flowing naturally" ,
    but the PL/SQL Challenge is doing a lot in this direction :) :) :)

    Thanks & Best Regards,

  3. @iudith

    Documentation explicitly states that explcit RETURN is mandatory in constructors and its returns SELF (Application Developer’s Guide - Object-Relational Features, 10.2, chapter "Defining and Implementing User-Defined Constructors"):

    The function body must include an explicit return; as shown. The return keyword
    must not be followed by a return expression. The system automatically returns the
    newly constructed SELF instance.

    As well an explanation of the SELF parameter is quite clear (at the same chapter):
    A user-defined constructor has an implicit first parameter SELF. Specifying this
    parameter in the declaration of a user-defined constructor is optional. If you do specify
    it, its mode must be declared to be IN OUT.