mariuss
mariuss

Reputation: 1247

Package specification and package body issue , illegal use of type before declaration

I have this issue that is confusing me.

I know that in order to use , let's say an associative array type as a procedure parameter or as a function's return type you need to create a package ; because associative arrays can only be used in PL/SQL blocks .

And so I did , this is my package schema :

CREATE OR REPLACE PACKAGE some_package AS

  TYPE vector IS TABLE OF INTEGER INDEX BY PLS_INTEGER;

  TYPE MATRIX IS TABLE OF vector INDEX BY PLS_INTEGER;

  PROCEDURE printMatrix (p_matrix IN MATRIX);

  FUNCTION BUILD_MATRIX(p_row_count IN INTEGER , p_column_count IN INTEGER) RETURN MATRIX;

END some_package;
/

My body package:

CREATE OR REPLACE PACKAGE BODY some_package AS

    PROCEDURE printMatrix (p_matrix IN MATRIX)
     IS

     BEGIN

         FOR i in p_matrix.first..p_matrix.last LOOP
            FOR j in p_matrix(i).first..p_matrix(i).last LOOP
               DBMS_OUTPUT.PUT_LINE(p_matrix(i)(j));
            END LOOP;
               DBMS_OUTPUT.PUT_LINE(CHR(10));
         END LOOP;

         DBMS_OUTPUT.PUT_LINE(CHR(10));

     END printMatrix;

  FUNCTION BUILD_MATRIX(p_row_count IN INTEGER , p_column_count IN INTEGER)
     RETURN MATRIX IS

     TYPE vector IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
     TYPE MATRIX IS TABLE OF vector INDEX BY PLS_INTEGER;

     v_my_matrix MATRIX;

     v_my_row vector;

     v_contor_row INTEGER;

     v_contor_column INTEGER;

     BEGIN

     FOR v_contor_row IN 0..p_row_count LOOP
         FOR v_contor_column IN 0..p_column_count LOOP
           v_my_row(v_contor_column) :=  0 + MOD(ABS(DBMS_RANDOM.RANDOM),30);
         END LOOP;

         v_my_matrix(v_contor_row) := v_my_row;

     END LOOP;

     return v_my_matrix;

     END;

END some_package;
/

But for some unknown reasons , I get these errors :

Error(9,12): PLS-00323: subprogram or cursor 'BUILD_MATRIX' is declared in a package specification and must be defined in the package body

Error(19,3): PL/SQL: Item ignored

Error(20,13): PLS-00498: illegal use of a type before its declaration

I don't undertand what I did wrong , the headers' types of my procedure and function are the same with the implementations : same parameters, same return type ... What am I doing wrong ?

Thank you for your time.

Upvotes: 1

Views: 1394

Answers (1)

krokodilko
krokodilko

Reputation: 36087

Just comment these declarations in the function body:

  FUNCTION BUILD_MATRIX(p_row_count IN INTEGER , p_column_count IN INTEGER)
     RETURN MATRIX IS

     -- TYPE vector IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
     -- TYPE MATRIX IS TABLE OF vector INDEX BY PLS_INTEGER;

     v_my_matrix MATRIX;

There are declared in the package, you don't need to "rededefine" them in the function.
If you declare them as local in this place with the same names as in the body, then they hide types declared in the package, and Oracle complains, because the function declaration from the package doesn't match with definition in the body (they use different types - even if their names are the same).

Upvotes: 3

Related Questions