String concatenation

Can you please tell me why comb1 gives an error in the following code, while comb2 does not? Thank you!


module top;
  string s1 = "hello";
  string s2 = "world";
  int    num = 65;
  string comb1 = {s1, " ", s2, num};
  //string comb2 = {"hello", " ", "world", num};
  initial begin
    $display("%0s",comb1);
    //$display("%0s",comb2);
  end
endmodule

In reply to jianfeng.he:

comb1 works for me.

hello worldA

In reply to sbellock:

Technically, they are both illegal according to the LRM. A concatenation must be one of

  • All integral expressions, resulting in an integral type
  • All string types or string literals, resulting
  • An unpacked array concatenation in the context of an assignment to an unpacked array.

A string literal is considered either as an integral type or string type depending on its context.

In the first concatenation, num is an integral type and cannot be concatenated with string types. It needs to be cast to a string type first.

In the second concatenation, all its operands are integral types, so the result is integral. But you cannot assign an integral type to a string type without a cast.

In reply to dave_59:

In reply to sbellock:
In the second concatenation, all its operands are integral types, so the result is integral. But you cannot assign an integral type to a string type without a cast.

Let me understand, are you saying that in comb2, the concatenation operator {} will treat “hello” and other strings as integers, resulting in an integer output? If we want to assign it to a string type comb2, we need to convert it using string’(). Is that correct?

In reply to jianfeng.he:

Correct. “hello” and “world” are both 40-bit integral values