# Static function usage in constraints

**URL:** <https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint, Static-function\
**Created:** [August 25, 2020, 11:49am UTC](https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991 "2020-08-25T11:49:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![visinescu13](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/visinescu13/32/3931_2.png) [@visinescu13](https://verificationacademy.com/forums/u/visinescu13)\
**Post date:** [August 25, 2020, 11:49am UTC](https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991/1 "2020-08-25T11:49:22Z")

</div>

Hello,

I try to use a static function inside a constraint but I see the following error:  
\*_xmsim: F,NWINFUNC (./testbench.sv,31|30): A new() was called by a function in a constraint. The unique id of the failed randomize call is 0._

My code from below can be run [here](https://www.edaplayground.com/x/3UbP):

```verilog

class static utils#(type T=int);
  
  static function bit exist(T q[$], T item);
    foreach(q[i])
      if(q[i] == item)
        return 1;
    return 0;
  endfunction
  
endclass

class test;
  rand int aux;
endclass

module tb;
  
  int q[$] = {1};
  string str[$] = {"hello"};
  
  test t1;
  
  initial begin
    $display(utils#(int)::exist(q,0));
    $display(utils#(int)::exist(q,1));
    $display(utils#(string)::exist(str,"hello"));
    
    $display(" *****TEST CLASS*****");
    t1 = new();
    void'(t1.randomize with {
      aux == utils#(int)::exist(q,1);
    });
    $display("t1.aux=%0d",t1.aux);
    #10;
    $finish(0);
  end
  
endmodule

```

Please let me know if it’s possible to use a static function inside a constraint.

Thank you,  
Eduard.

---

<div class="post-metadata">

**Author:** ![ssureshgverifier](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/bcef8e/32.png) [@ssureshgverifier](https://verificationacademy.com/forums/u/ssureshgverifier)\
**Post date:** [August 25, 2020, 4:43pm UTC](https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991/2 "2020-08-25T16:43:34Z")

</div>

_In reply to [visinescu13](https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991):_

works fine on Mentor Questa and VCS

> **[EDA Playground](https://www.edaplayground.com/x/5bZ8)**
>
> Edit, save, simulate, synthesize SystemVerilog, Verilog, VHDL and other HDLs from your web browser.

I had to remove ‘static’ from class definition,

```verilog

class utils#(type T=int);

```

but, for what you want to achieve, you can put this function in post randomize

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 25, 2020, 4:55pm UTC](https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991/3 "2020-08-25T16:55:04Z")

</div>

_In reply to [visinescu13](https://verificationacademy.com/forums/t/static-function-usage-in-constraints/37991/2):_

You’ve run into a tool specific problem, and an LRM issue. Obviously there is no call to new in the function, so the error is a bug. Report that to your tool vendor.

There is not supposed to be a class lifetime in the BNF. That is an [LRM bug](https://accellera.mantishub.io/view.php?id=3561) that got resolved, but the fix was skipped from incorporation.
