Jan Doggen
Jan Doggen

Reputation: 9106

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.

Upvotes: 2

Views: 5434

Answers (2)

iMan Biglari
iMan Biglari

Reputation: 4786

You can turn that specific warning off. It's just a reminder, and most of the time your program works fine. I guess the compiler still sees String as ShortString in the functions which are built into it like Str() and Writeln().

Upvotes: 0

user743382
user743382

Reputation:

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.

Upvotes: 9

Related Questions