Compile-time Polymorphism in SV

I understand SV doesn’t support compile-time polymorphism i.e. function overloading evidently.

Look at the example below and please help me understand two queries

  1. Is this not a candidate of ‘function overloading’ as the signature of the function ‘add’ in the base class and derived class are different?

  2. If yes, is this supported accidentally or conciously? Note, this is compile-error free and can be executed.


class Base;

  function int add (int a, 
                    int b);

    return (a + b);

  endfunction:add

endclass:Base


class Derived extends Base;

  function int add (int a, 
                    int b,
                    int c);

    return (super.add (a,b) + c);

  endfunction:add

endclass:Derived

module tb;

  Derived d_obj = new ();

  initial
  begin
    $display ("Sum : %0d", d_obj.add (1,2,3));
  end

endmodule:tb

Thanks,
Prem

In reply to run2prem:

Function overloading would be calling different implementations of a function based on the signature alone. Your code calls Derived::add based on the type of class variable d_obj. You must call it with 3 arguments. This is because add is not a virtual method.

In reply to dave_59:

Hi Dave,

You must call it with 3 arguments
Yes got it and agreed.

But within the ‘Derived’ class scope, we get two ‘add’ methods with different signatures. Yes, of course user has to resolve the call himself by specifying this. or super.

My query is

  1. This is a case of polymorphism or not?

  2. If so, is this function overloading or overriding?

I’m not sure how C++ or other OOP language handles this.

Thanks,
Prem

In reply to run2prem:

This is not polymorphism. Polymorphism is the same code behaving differently under different circumstances. SystemVerilog has compile time, or static polymorphism when you parameterize a module or class. Run-Time or dynamic polymorphism is implemented with virtual methods.

What you have done is override the add function in the Derived class. This “hides” the original add function.

You may want to read: Parameterized Classes, Static Members and the Factory Macros - Verification Horizons

In reply to dave_59:

Thanks, Dave!

Regards,
Prem