1 2 Previous Next 19 Replies Latest reply: Jul 23, 2009 11:23 AM by Hoek Go to original post RSS
      • 15. Re: plsql object type polymorphism
        APC
        Furthermore as far as I can tell it is not possible to find out what sub-type an object is from with PL/SQL, so you couldn't
        even use Padders's TREAT ploy.
        I spoke too soon. I have since read through the [11g OO App Developer’s Guide|http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#i479093] and discoverd we can now use IS OF in PL/SQL.

        So, this is now possible in 11g....
         SQL> declare
          2      my_aviary birds_nt := birds_nt();
          3  begin
          4      my_aviary.extend(5);
          5      my_aviary(1) := duck('Donald');
          6      my_aviary(2) := chicken('Chicken Little');
          7      my_aviary(3) := canary('Tweetie Pie');
          8      my_aviary(4) := rooster('Foghorn Leghorn');
          9      my_aviary(5) := bird('?');
         10      for idx in my_aviary.first()..my_aviary.last()
         11      loop
         12          dbms_output.put_line( my_aviary(idx).fly() );
         13          if my_aviary(idx) is of (duck) then
         14              dbms_output.put_line( treat(my_aviary(idx) as duck).quack() );
         15          elsif my_aviary(idx) is of (chicken) then
         16              dbms_output.put_line( treat(my_aviary(idx) as chicken).cluck() );
         17          else
         18              dbms_output.put_line( my_aviary(idx).make_a_noise() );
         19          end if;
         20      end loop;
         21  end;
         22  /
        my name is Donald
        quack! quack! QUACK!!
        my name is Chicken Little
        cluck-cluck-cluck
        my name is Tweetie Pie
        I taught I taw a puddy tat!!!
        my name is Foghorn Leghorn
        Doo dah, doo dah
        my name is ?
        generic twitter
        
        PL/SQL procedure successfully completed.
        
        SQL> 
        This may also work in 10g, I don't know.

        By the way you may have noticed that I have modified the objects slightly from my previous example. Duck now has a quack() member function, Chicken has a cluck() and Rooster overrides the cluck(). I haven't bothered to list the code here, you're all bright enough to figure it out. The cool thing to note is that the instance of Rooster used its version of cluck() not the Chicken one.

        Cheers, APC

        blog: http://radiofreetooting.blogspot.com

        Edited by: APC on Jul 23, 2009 9:03 AM

        Gosh-darned apostrophes in HTML. No wonder nobody uses them any more!
        • 16. Re: plsql object type polymorphism
          94799
          I would have thought the chicken would have clucked more whilst being overridden by the rooster.
          • 17. Re: plsql object type polymorphism
            713310
            Thanks guys.

            It looks like my original code is just simply not valid under Oracle's OO extensions ... A little disheartening, but oh well.

            APC, thanks for the examples.

            Have a good one guys!
            • 18. Re: plsql object type polymorphism
              APC
              It looks like my original code is just simply not valid under Oracle's OO extensions ... A little disheartening, but oh well.
              Yeah, well Oracle - being a database company at heart - is firmly in favour of strong typing. So it's not surprising that its OO implementation doesn't support [ Python-esque duck-typing|http://en.wikipedia.org/wiki/Duck_typing]. I'm not obsessed with ducks, honest I'm not.

              Cheers, APC

              blog: http://radiofreetooting.blogspot.com

              Edited by: APC on Jul 24, 2009 10:25 AM

              fixed link formatting
              • 19. Re: plsql object type polymorphism
                Hoek
                I'm not obsessed with ducks, honest I'm not.
                Hmz, so you're probably hiding your chicken obsession by diverting/distracting us all with the 'Duckish thingies'? ;)

                silently continues working on his 'Hello Kittie polymorphic-thing' in complete denial as well, while listening to Mozart
                1 2 Previous Next