Equality Constraint on Class Objects

Is it possible to use a Equality Constraint on objects of the same class ( class has random properties ) ?



module Assigning_Class_Handles ;


 class C ;

  rand bit [1:0] a , b ;
 
 endclass


 class Main ;

  rand C c1 , c2 ;

  function void pre_randomize() ;

    c1 = new();
    c2 = new();

  endfunction

 
  constraint EQUALITY { c1 == c2 ; }

 endclass 

 Main m ;

 initial begin

 m = new() ;

 repeat(3)
 if ( m.randomize() )
  begin
   $display("Success with %p",m);
  end
else
  begin
   $display("Fails");
  end

 end

endmodule



I see randomization failure for this .

Since expression within a Constraint should be an Integral Expression , does the constraint violate this ?

Shouldn’t the Equality Operator make it an integral expression ?

In reply to TC_2017:

The problem is the equality expression is comparing two class variables and the handles they contain, not a deep comparison of all its members. Also, the constraint solver will never modify a random class variable, only integral class member variables.

In reply to dave_59:

Hi Dave ,

I got the first part of your reply i.e the reason for the failure .

Could you please elaborate on the 2nd statement ?

I remember reading in the LRM about "constraint shall not modify the actual object handle "

Never understood the exact meaning for it.

In reply to TC_2017:

You might want to read this post about class terminology. The constraint solver can only make assignments to integral variables. It can also change the size of a dynamic array, but if that is an array of class variables, there is no way to modify any element. That’s why you must construct all elements first, and the solver can shrink by deleting elements.

In reply to TC_2017:

Looks like you are looking for a shortcut to constrain all the fields of 2 objects. Basically a “deep equality”. SV doesn’t have a construct to do that, so you should explain why you want to do this.

Instead of randomizing both objects, would only creating one in pre_randomize(), following by cloning it in post_randomize() work?

In reply to warnerrs:

**Actually the code was from learning perspective rather than practical application .
**
Since equality Operator are used to constraint 2 random properties to have same value ,
there would be many ( including me ) to think that it would do a deep copy of values

The way to actually achieve it would as you suggested .
Constraint only 1 object properties and then do a deep copy in post_randomize()