Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
50
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

How Subprogram Calls Are Resolved

You cannot overload two subprograms if their formal parameters differ only in name or parameter mode. For example, you cannot overload the following two procedures:

DECLARE

PROCEDURE reconcile (acct_no IN INTEGER) IS BEGIN NULL; END;

PROCEDURE reconcile (acct_no OUT INTEGER) IS BEGIN NULL; END;

/

You cannot overload subprograms whose parameters differ only in subtype. For example, you cannot overload procedures where one accepts an INTEGER parameter and the other accepts a REAL parameter, even though INTEGER and REAL are both subtypes of NUMBER and so are in the same family.

You cannot overload two functions that differ only in the datatype of the return value, even if the types are in different families. For example, you cannot overload two functions where one returns BOOLEAN and the other returns INTEGER.

How Subprogram Calls Are Resolved

Figure 8–1 shows how the PL/SQL compiler resolves subprogram calls. When the compiler encounters a procedure or function call, it tries to find a declaration that matches the call. The compiler searches first in the current scope and then, if necessary, in successive enclosing scopes. The compiler looks more closely when it finds one or more subprogram declarations in which the subprogram name matches the name of the called subprogram.

To resolve a call among possibly like-named subprograms at the same level of scope, the compiler must find an exact match between the actual and formal parameters. They must match in number, order, and datatype (unless some formal parameters were assigned default values). If no match is found or if multiple matches are found, the compiler generates a semantic error.

The following example calls the enclosing procedure swap from the function reconcile, generating an error because neither declaration of swap within the current scope matches the procedure call:

PROCEDURE swap (n1 NUMBER, n2 NUMBER) IS num1 NUMBER;

num2 NUMBER;

FUNCTION balance (...) RETURN REAL IS

PROCEDURE swap (d1 DATE, d2 DATE) IS BEGIN NULL; END; PROCEDURE swap (b1 BOOLEAN, b2 BOOLEAN) IS BEGIN NULL; END; BEGIN

swap(num1, num2); RETURN ...

END balance; BEGIN NULL; END;

/

8-12 PL/SQL User's Guide and Reference

How Subprogram Calls Are Resolved

Figure 8–1 How the PL/SQL Compiler Resolves Calls

encounter

 

subprogram call

 

compare name of

 

called subprogram with

go to enclosing scope

names of any

subprograms declared

 

in current scope

 

 

Yes

match(es) found?

No

enclosing scope?

Yes

No

compare actual

 

parameter list in

 

subprogram call with

 

formal parameter list in

 

subprogram declaration(s)

 

No

match(es) found?

Yes

Yes

multiple matches?

No

resolve call

 

generate semantic error

 

 

 

How Overloading Works with Inheritance

The overloading algorithm allows substituting a subtype value for a formal parameter that is a supertype. This capability is known as substitutability. If more than one instance of an overloaded procedure matches the procedure call, the following rules apply to determine which procedure is called:

If the only difference in the signatures of the overloaded procedures is that some parameters are object types from the same supertype-subtype hierarchy, the closest match is used. The closest match is one where all the parameters are at least as close as any other overloaded instance, as determined by the depth of inheritance between the subtype and supertype, and at least one parameter is closer.

A semantic error occurs when two overloaded instances match, and some argument types are closer in one overloaded procedure to the actual arguments than in any other instance.

Using PL/SQL Subprograms 8-13

How Subprogram Calls Are Resolved

A semantic error also occurs if some parameters are different in their position within the object type hierarchy, and other parameters are of different datatypes so that an implicit conversion would be necessary.

For example, here we create a type hierarchy with 3 levels:

CREATE TYPE super_t AS object (n NUMBER) NOT final;

CREATE OR replace TYPE sub_t under super_t (n2 NUMBER) NOT final;

CREATE OR replace TYPE final_t under sub_t (n3 NUMBER);

We declare two overloaded instances of a function, where the only difference in argument types is their position in this type hierarchy:

CREATE PACKAGE p IS

FUNCTION foo (arg super_t) RETURN NUMBER; FUNCTION foo (arg sub_t) RETURN NUMBER;

END;

/

CREATE PACKAGE BODY p IS

FUNCTION foo (arg super_t) RETURN NUMBER IS BEGIN RETURN 1; END; FUNCTION foo (arg sub_t) RETURN NUMBER IS BEGIN RETURN 2; END;

END;

/

We declare a variable of type final_t, then call the overloaded function. The instance of the function that is executed is the one that accepts a sub_t parameter, because that type is closer to final_t in the hierarchy than super_t is.

set serveroutput on declare

v final_t := final_t(1,2,3); begin

dbms_output.put_line(p.foo(v)); end;

/

In the previous example, the choice of which instance to call is made at compile time. In the following example, this choice is made dynamically.

CREATE TYPE super_t2 AS object

(n NUMBER, MEMBER FUNCTION foo RETURN NUMBER) NOT final;

/

CREATE TYPE BODY super_t2 AS

MEMBER FUNCTION foo RETURN NUMBER IS BEGIN RETURN 1; END; END;

/

CREATE OR replace TYPE sub_t2 under super_t2 (n2 NUMBER,

OVERRIDING MEMBER FUNCTION foo RETURN NUMBER) NOT final;

/

CREATE TYPE BODY sub_t2 AS

OVERRIDING MEMBER FUNCTION foo RETURN NUMBER IS BEGIN RETURN 2; END;

END;

/

CREATE OR replace TYPE final_t2 under sub_t2 (n3 NUMBER);

/

8-14 PL/SQL User's Guide and Reference

Соседние файлы в папке Oracle 10g