Can we can have print statement in classes without any function/task in SystemVerilog?
http://www.edaplayground.com/x/8Y8
class A;
int x=10;
$display("x=%d",x);
endclass
module abc;
A a;
initial begin
a=new();
end
endmodule
Can we can have print statement in classes without any function/task in SystemVerilog?
http://www.edaplayground.com/x/8Y8
class A;
int x=10;
$display("x=%d",x);
endclass
module abc;
A a;
initial begin
a=new();
end
endmodule
No. According to the IEEE Std 1800-2012, section 8.3 "Syntax", a class may only contain the following items:
class_item ::=
{ attribute_instance } class_property
| { attribute_instance } class_method
| { attribute_instance } class_constraint
| { attribute_instance } class_declaration
| { attribute_instance } covergroup_declaration
| local_parameter_declaration ;
| parameter_declaration7 ;
| ;
A $display
statement may only occur in a procedural block (initial, always, final) or a task/function body.
Typically, you need to create a display function in the class, then call it explicitly.
This is not possible. Either you have to print in a dedicated task/function or in the class constructor.
LRM states that: A display/monitor/strobe/write statement may only occur in a procedural block or in a task/function body.
We can display the default values of variables, when object is created as follows. A better option is using display in constructor itself.
class A;
int x=10;
function void new(); // constructor
$display("x=%d",x);
endfunction
endclass