Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Str() give "W1057 Implicit string cast from 'ShortString' to 'string'"?

Consider:

function x_StrZero(N: Double; W: Integer; D: Integer = 0): String;
var S : String;
begin
  Str(N:W:D,S);   
  S := Trim(S);

This gives W1057 Implicit string cast from 'ShortString' to 'string'

The online doc says:

procedure Str(const X [: Width [:Decimals]]; var S: String);

but also

Notes: However, on using this procedure, the compiler may issue a warning: W1057 Implicit string cast from '%s' to '%s' (Delphi).

Why would this be?

I would like to prevent this ugly workaround:

function x_StrZero(N: Double; W: Integer; D: Integer = 0): String;
var
  S : String;
  SS : ShortString;
begin
  Str(N:W:D,SS);
  S := Trim(String(SS));

I have read Why does Delphi warn when assigning ShortString to string? but that does not answer this.

like image 507
Jan Doggen Avatar asked Jan 30 '26 07:01

Jan Doggen


1 Answers

Str(N:W:D,S);   

gets compiled as

S := System._Str2Ext(N, W, D);

where System._Str2Ext is a function with a return type of ShortString. It gets converted to string in the assignment to S. The warning, while not easily readable, is correct, there is an implicit conversion at that point. So either rework the code to not have an implicit conversion there by avoiding Str, or turn off the warning, or ignore the warning.