Oracle How to Chained Method Invocation PL/SQL

Viewed 59

Using PL/SQL, how does one chain two method calls in the same statement without assigning an output to some variable in the calling scope?

I first saw this technique in utPLSQL framework.

begin
  ut.expect(6/2).to_equal(3);
end;
/

This db<>fiddle shows my various failed attempts, but the primary statements are copied below.

create or replace type obj as object 
  ( var number
  , member function fn(self in out obj, val number) return obj
  , member procedure proc(val number)
  )
;
/
create or replace type body obj
as
  member function fn(self in out obj, val number)
    return obj
    as
    begin
      self.var := val;
      return self;
    end
  ;
  
  member procedure proc(self in out obj, val number)
    as
    begin
      self.var := self.var * val;
    end
  ;
end;
/
declare
  o obj;
begin
  o := obj(4);
  o.fn(5).proc(2); --PLS-00363: expression 'FIDDLE_ZCGFGJIACZWGMWNQLREX.OBJ.FN(O, 5)' cannot be used as an assignment target
  dbms_output.put_line(o.var);
end;
/

I am expecting 10 to display (the result of 5 * 2) instead of the PLS-00363.

Using Oracle 19c Enterprise Edition (though the db<>fiddle is 21c).

Thank you in advance.

0 Answers
Related