Debugging constraint failure for unpacked_array.sum()

Hi All,

I was trying to write a constraint for following intention

For an unpacked array of size 4, 2 elements should be unique

Here is my attempt

 class my_class;   
   
 rand bit [1:0] a[4];
 rand bit [1:0] b;
  
  constraint SUM  {

// Constraints 'b' twice but doesn't constraint other 2 elements as unique !!
    a.sum() with ( int'(item == b) ) == 2; 

//  Trial1 :: Works !!        
    foreach(a[i]) 
      if( a[i] != b )
      a.sum() with ( int'(item == a[i]) ) == 1;

/*
//  Trial2 :: Moved the if condition within with clause    
    foreach(a[i])   
     a.sum() with ( (a[i] != b) ? int'(item == a[i]) : 0 ) == 1;
*/
    
                  }

  constraint B {
                 b inside {a};
               }
    
endclass

I observe that Trial1 works whereas Trial2 doesn’t

(1) I am not clear why randomization fails for Trial2. Any suggestions ?

(2) Is it possible to meet the intention without any helper variable ?

Thanks