External constraint in randomization

Hi All,
I am using the externel constraint for my knowledge checking.But i am faced error and my code is below

class Packet;
  rand bit [7:0] length;
  rand bit [7:0] payload[];
  constraint c_valid {length > 0;
  payload.size() == length;}
  constraint c_external;
  extern function void display(); 
endclass

function void Packet:: display();
   $display("length=%d payload=%d",length,payload.size() );
endfunction


// test.sv
program test;
  Packet p;
  initial begin
    p=new();
    p.display();
    assert(p.randomize());
    constraint Packet::c_external {length == 1;};
    p.display();
  end
endprogram

ERROR:
** Error: ext_constraint.sv(22): near “constraint”: syntax error, unexpected constraint

The problem is an extern constraint body must be defined in the same scope as the class declaration.

Thanks for reply!
I have a one doubt. If i am declare the extern constraint in class declaration,then what is the use of the extern constraint?

In reply to Rajaraman R:

As Dave pointed out, it should be in the same scope as class declaration.



// Code your testbench here
// or browse Examples
class Packet;
  rand bit [7:0] length;
  rand bit [7:0] payload[];
  constraint c_valid {length > 0;
  payload.size() == length;}
  extern constraint c_external;
  extern function void display(); 
endclass
 
function void Packet:: display();
   $display("length=%d payload=%d",length,payload.size() );
endfunction
 
    constraint Packet::c_external {length == 1;}
 
// test.sv
program test;
  Packet p;
  initial begin
    p=new();
    
    p.display();
    
    assert(p.randomize());
    
    p.display();
  end
endprogram


In reply to Rajaraman R:

It serves the same purpose of extern methods. By operating the prototype declaration from their implementation, you can can hide the implementation by encrypting it, or protect it be putting it in a separate file that cannot be modified. Not as useful with constraints as it is for methods.