Recent

Author Topic: {$mode objfpc} makes compiler treat "string" as "shortstring  (Read 3110 times)

MISV

  • Hero Member
  • *****
  • Posts: 815
{$mode objfpc} makes compiler treat "string" as "shortstring
« on: February 01, 2019, 12:18:06 am »
I have a unit which at top defines
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}

I then have a function

Code: Pascal  [Select][+][-]
  1. function mytest: string;
  2. begin
  3.   other_function_in_other_normal_unit(Result);
  4. end;
  5.  
...

In another unit I then have

function other_function_in_other_normal_unit(var S: string);
begin
  S := 'try long string over 255 chars';
end;

...

You will now get error
Quote
Got "ShortString" expected "AnsiString"

...

It was lucky I tried the above because it was driving me nuts my strings were "cut" without I understood why :)

Is the above intended behavior? Or a bug? (FPC 3.0.4)

MISV

  • Hero Member
  • *****
  • Posts: 815
Re: {$mode objfpc} makes compiler treat "string" as "shortstring
« Reply #1 on: February 01, 2019, 12:52:00 am »
Apparently it as intended
http://wiki.freepascal.org/Mode_ObjFPC

I am not sure I understand
Quote
is the default mode for Lazarus source files
since I have *never* experience Lazarus default to consider string for shortstrings (But then again I started most of my projects as Delphi projects so....)

Anyhow... I need ObjFPC for some routines + need long strings... So I suppose I should simply use {$H+} as well. I will try that.
« Last Edit: February 01, 2019, 12:55:14 am by MISV »

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: {$mode objfpc} makes compiler treat "string" as "shortstring
« Reply #2 on: February 01, 2019, 12:56:08 am »
What about "another unit", does it have {$mode objfpc} too?
What about "Use Ansistrings" in project->compiler->parsing?

Looks like inconsistent string type across units in project.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

440bx

  • Hero Member
  • *****
  • Posts: 6155
Re: {$mode objfpc} makes compiler treat "string" as "shortstring
« Reply #3 on: February 01, 2019, 12:56:23 am »
So I suppose I should simply use {$H+} as well. I will try that.
That is correct.  In objfpc mode you have to enable longstrings separately.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

MISV

  • Hero Member
  • *****
  • Posts: 815
Re: {$mode objfpc} makes compiler treat "string" as "shortstring
« Reply #4 on: February 01, 2019, 11:32:53 am »
My projects always used and depended on longstrings without my units contain {$H+} at top.

(However, maybe I am using longstrings since my projects been imported from Delphi, so... Maybe somehow a setting from there has trickled down)

(Hard for me to imagine anyone actually uses string = shortstring as default.)

But anyhow - the issue was fully resolved by including {$H+} after {$objfpc} - thank you everyone!


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: {$mode objfpc} makes compiler treat "string" as "shortstring
« Reply #5 on: February 01, 2019, 12:51:15 pm »
Apparently it as intended
http://wiki.freepascal.org/Mode_ObjFPC

I am not sure I understand

Like most of objfpc's artifacts, it is historic. Back then (1999-2000), ansistrings were new, and there were benchmarks that performance degraded (because of the dynamic allocation). Yet people wanted the new object model.

Lazarus always generated {$mode objfpc}{$H+} to get around this.


Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: {$mode objfpc} makes compiler treat "string" as "shortstring
« Reply #6 on: February 01, 2019, 03:13:02 pm »
Correct, but
- Back then is 1994-1995 in the Delphi case
- There was a huge clutter about the speed even then (I was a very early beta-tester) .
- It is not legacy: if you need speed use shortstrings.
- Current {$mode delphi} is always {$H+} unless defines {$H-}
- I do not understand why Lazarus needs {$mode objfpc}, luckily it actually doesn't.... 8-)
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018