5

I'm trying to port a project from Delphi 4 to Delphi XE2. I have a requirement to use shortstring in the project. According to the Delphi Help, $H- should make the compiler use shortstrings for the string type. I've used this directive but I don't see any difference. I've written a small test program:

program Stringtest;

{$APPTYPE CONSOLE}

{$R *.res}
{$H- }
uses
  System.SysUtils;

var
 str : string;
 short : shortstring;
begin
  try
    str := 'testing';
    short := 'testing';
    Writeln('str ' +Format('%d', [sizeOf(str)]) );
    Writeln('short  ' +Format('%d', [sizeOf(short)])  );
    Readln; 
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

In my opinion the output should be the same for both str and short because the compiler should treat both as a shortstring. But the size is 4 for str and 256 for short. Is there any other way to have the compiler treat string as shortstring or is the only solution to replace all occurences of string with shortstring in the source code?

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Maria
  • 5,574
  • 1
  • 29
  • 39
  • 1
    `$H-` is legacy, provided only for backward compatibility. There is no way to force the Delphi XE2 (or any version of Delphi's compiler from D2009 and higher) to not use Unicode as the default string type. You'll need to explicitly change all of your string references to shortstring (or better yet, fix your code to not require them). – Ken White Mar 06 '12 at 13:46
  • @KenWhite Thank you for your answer. That confirms my suspicions. In the [help section](http://docwiki.embarcadero.com/RADStudio/XE2/en/String_Types) for String Type of Delphi XE 2, it is written that the $H- directive could be used for that purpose. But I guess that is outdated then. – Maria Mar 06 '12 at 14:37
  • it's there just to prevent old code from breaking when compiled. There was a bit of an uproar when Delphi 2009 was released with Unicode-only as the default string type, without a switch to revert back to having AnsiString as the default. It was explained that they determined this would be virtually impossible due to the need to have two different versions of the RTL, VCL, and so forth. Allowing ShortString to become the default would require the same thing. – Ken White Mar 06 '12 at 15:25
  • @KenWhite, Why not post your comment as an answer? – RRUZ Mar 06 '12 at 15:55
  • @ken yes, please make these comments into an answer – David Heffernan Mar 06 '12 at 16:41
  • Note that in Delphi 5 or 6, the compiler changed its handling of ShortString operations. A ShortString would be converted to an AnsiString, the operation performed on that temporary value, and the result converted back to ShortString. So even if you could continue to use ShortString, it probably wouldn't be satisfactory. The version of Delphi you're coming from is about 14 years old. Expect to have to change a few things to catch up. – Rob Kennedy Mar 06 '12 at 16:46

1 Answers1

7

$H- is legacy, provided only for backward compatibility. There is no way to force the Delphi XE2 (or any version of Delphi's compiler from D2009 and higher) to not use Unicode as the default string type.

The compiler directive is still there just to prevent old code from breaking when compiled. There was a bit of an uproar when Delphi 2009 was released with Unicode-only as the default string type, without a switch to revert back to having AnsiString as the default. It was explained that they determined this would be virtually impossible due to the need to have two different versions of the RTL, VCL, and so forth. Allowing ShortString to become the default would require the same thing.

You'll need to explicitly change all of your string references to shortstring (or better yet, fix your code to not require them).

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • IMO yet another stupid decision from Embarcadero. If the code uses {$H-} there's big chance you'll have to fix it anyway; parsing {$H-} without warnings is just making it harder for the programmer :-/ – Flávio Etrusco Jul 25 '13 at 14:10