Constraints and integral expressions

Hello,

I used a helper array to get(randomize) the skittle array that is of enum type (since constraints work on integral expressions). But I still see an error:

typedef enum {A, B, C, D}skittles_e;
class Example;
   skittles_e skittle[10];
   rand bit[3:0] skittle_int[10];
  
  constraint c1{skittle_int dist {1:=20, 2:=20, 3:=10, 4:=50};}
  constraint c2{foreach (skittle_int[i]) {
    skittle_int[i] inside {1, 2, 3, 4};
  }};

    function void post_randomize();
      foreach(skittle_int[i]) begin
        if(skittle_int[i] == 1) skittle[i] = A;
        if(skittle_int[i] == 2) skittle[i] = B;
        if(skittle_int[i] == 3) skittle[i] = C;
        if(skittle_int[i] == 4) skittle[i] = D;
     end
     $display("skittle = %p", skittle);
  endfunction
endclass

module TB;
	Example E;

	initial begin
		E = new();
		repeat(2)
		  assert(E.randomize());
	end
endmodule

Error:
The variable skittle_int is not an integral type.
Change the type of the variable or remove it from the constraint expression.

Any idea what am I missing here? Thanks!

Your error is in constraint c1, where you are attempting to constrain an entire array instead of constraining each element.

Why are you using a helper array? Enumerated types are integral and can be used in constraints:

typedef enum {A, B, C, D} skittles_e;
class Example;
  rand skittles_e skittle[10];
  
  constraint c1 {
    foreach (skittle[i]) {
      skittle[i] dist {A:=20, B:=20, C:=10, D:=50};
    }
  };

  function void print_skittles();
    $display("skittle = %p", skittle);
  endfunction
endclass

module TB;
  Example E;
  
  initial begin
    E = new();
    repeat(2) begin
      if (!E.randomize()) begin
        $display("Randomization failed");
      end
      else begin
        E.print_skittles();
      end
    end
  end
endmodule