Recent

Author Topic: String Overloading / $LongStrings  (Read 6780 times)

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
String Overloading / $LongStrings
« on: July 05, 2016, 09:26:35 am »
Given this code:
Code: Pascal  [Select][+][-]
  1. program test_strings;
  2. {$mode ObjFPC}
  3. {$LongStrings OFF}
  4. var
  5.    my_string : String = 'hello there';
  6.  
  7. procedure PrintString(message : String);
  8. begin
  9.     Writeln('from String version:',message);
  10. end;
  11.  
  12. procedure PrintString(message : ShortString);
  13. begin
  14.     Writeln('from ShortString version:',message);
  15. end;
  16.  
  17. procedure PrintString(message : AnsiString);
  18. begin
  19.     Writeln('from AnsiString version:',message);
  20. end;
  21.  
  22. begin
  23.    PrintString('this is a string literal');
  24. end.

The following compile errors occur:
Quote
fpc test_strings.pp
test_strings.pp(12,11) Error: overloaded functions have the same parameter list
test_strings.pp(7,11) Error: Found declaration: PrintString(ShortString);
test_strings.pp(30,2) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/local/bin/ppcx64 returned an error exitcode

That makes sense since ShortString and String are supposed to be synonymous with {$LongStrings OFF}.

But if I change it to {$LongStrings ON} the errors are:

Quote
fpc test_strings.pp
test_strings.pp(17,11) Error: overloaded functions have the same parameter list
test_strings.pp(7,11) Error: Found declaration: PrintString(AnsiString);
test_strings.pp(12,11) Error: Found declaration: PrintString(ShortString);
test_strings.pp(30,2) Fatal: There were 3 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/local/bin/ppcx64 returned an error exitcode
user@XPS13_DevEd:~/code/pascal$

It is referring to the String function as an AnsiString function and says it conflicts with the ShortString function and the AnsiString function, rather than just saying that it conflicts with the AnsiString version. The same behavior occurs with {$H+}/{$H-}

So it appears that {$LongStrings ON} turns both String and ShortString into AnsiString, but the documentation for it only talks about String, and String used without brackets for a length.

« Last Edit: July 05, 2016, 09:32:01 am by Pasqualish »

Awkward

  • Jr. Member
  • **
  • Posts: 66
Re: String Overloading / $LongStrings
« Reply #1 on: July 05, 2016, 09:38:07 am »
Try to remove wrong function definition (doubling, with same argument) and compile then

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: String Overloading / $LongStrings
« Reply #2 on: July 05, 2016, 09:59:03 am »
Try to remove wrong function definition (doubling, with same argument) and compile then

If I take out the AnsiString function (and {$LongStrings ON}) it compiles. It could be that the error message is not pointing to a conflict with the ShortString version, but just throwing it in as a member of the overloaded group.

It appears that with {$LongStrings ON} a string literal becomes an AnsiString, whereas, if {$LongStrings OFF} is in play, string literals are ShortStrings.
« Last Edit: July 05, 2016, 10:13:46 am by Pasqualish »

Awkward

  • Jr. Member
  • **
  • Posts: 66
Re: String Overloading / $LongStrings
« Reply #3 on: July 05, 2016, 10:03:52 am »
Yes, we can avoid error by code changing but still get not 100% proper error message, agree

Thaddy

  • Hero Member
  • *****
  • Posts: 10482
Re: String Overloading / $LongStrings
« Reply #4 on: July 05, 2016, 12:18:44 pm »
It appears that with {$LongStrings ON} a string literal becomes an AnsiString, whereas, if {$LongStrings OFF} is in play, string literals are ShortStrings.
Yes, and that is also the desired/requested behavior. I don' t see the problem. With {$H-}/{$LONGSTRINGS OFF} the default string type is a short string.
Which makes it also the default for string literals.
« Last Edit: July 05, 2016, 12:58:13 pm by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: String Overloading / $LongStrings
« Reply #5 on: July 05, 2016, 02:58:43 pm »
It appears that with {$LongStrings ON} a string literal becomes an AnsiString, whereas, if {$LongStrings OFF} is in play, string literals are ShortStrings.
Yes, and that is also the desired/requested behavior. I don' t see the problem. With {$H-}/{$LONGSTRINGS OFF} the default string type is a short string.
Which makes it also the default for string literals.

Just stating what I appeared to observe, not making a complaint. Although I  guess I would have preferred "" for multi-byte-per-char strings and '' to be used for one-byte-per-char strings, assuming that double-quotes don't already have a purpose. But I see that one can cast the literal to get it to be the non-default (e.g. ShortString('hello') ) .
« Last Edit: July 05, 2016, 04:08:21 pm by Pasqualish »

Thaddy

  • Hero Member
  • *****
  • Posts: 10482
Re: String Overloading / $LongStrings
« Reply #6 on: July 05, 2016, 05:15:01 pm »
Quote
the non-default (e.g. ShortString('hello') ) .

No. The default in the default fpc mode is shortstring, also known as Pascal string. So it is the default... 8-), not the non-default... :D
And such a typecast is highly dangerous in other modes, where {$LONGSTRINGS ON} is used: your original string will be truncated to 255 bytes even if it is 20.000 characters long.
Don' t do that.
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: String Overloading / $LongStrings
« Reply #7 on: July 05, 2016, 05:40:36 pm »
Quote
the non-default (e.g. ShortString('hello') ) .

No. The default in the default fpc mode is shortstring, also known as Pascal string. So it is the default... 8-), not the non-default... :D
And such a typecast is highly dangerous in other modes, where {$LONGSTRINGS ON} is used: your original string will be truncated to 255 bytes even if it is 20.000 characters long.
Don' t do that.

When I said default, I meant "the current default in the program for what a string literal translates to". I was also only referring to casting string literals not variables. This became an issue because I was somehow under the impression that AnsiStrings, despite the name, did not store AnsiChars, but rather, WideChars. But in re-looking at the documentation, it appears that they are single byte, as they show up under single-byte strings, and WideStrings are used for Unicode. Not sure how WideStrings would be initialized with string literals though.
« Last Edit: July 05, 2016, 06:44:13 pm by Pasqualish »

Thaddy

  • Hero Member
  • *****
  • Posts: 10482
Re: String Overloading / $LongStrings
« Reply #8 on: July 05, 2016, 06:55:06 pm »
s became an issue because I was somehow under the impression that AnsiStrings, despite the name, did not store AnsiChars, but rather, WideChars. But in re-looking at the documentation, it appears that they are single byte, as they show up under single-byte strings, and WideStrings are used for Unicode. Not sure how WideStrings would be initialized with string literals though.
Under either {$modeswitch unicodestrings} or {$mode delphiunicode} the string literals are unicode strings in the UTF16 format.
Otherwise the default is shortstring unless {$H+} or the equivalent {$LONGSTRINGS ON}

Also note there is no such thing as an unicode shortstring. These are always Ansi, i.e. single byte chars.

« Last Edit: July 05, 2016, 06:58:08 pm by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: String Overloading / $LongStrings
« Reply #9 on: July 06, 2016, 11:37:20 am »
{$modeswitch unicodestrings}
{$mode delphiunicode} the string literals are unicode strings in the UTF16 format.

Neither of those is listed in the current online documentation. I assume they are new as of 3.0.0 and the documentation has not been updated.

I saw an odd thing on the wiki with regard to an alleged new mode {$mode iso}. It becomes effective as of FPC version 3.0.2.  I thought a version change at the third digit was bug fixes only.
« Last Edit: July 06, 2016, 11:40:34 am by Pasqualish »

Thaddy

  • Hero Member
  • *****
  • Posts: 10482
Re: String Overloading / $LongStrings
« Reply #10 on: July 06, 2016, 11:50:03 am »

Neither of those is listed in the current online documentation. I assume they are new as of 3.0.0 and the documentation has not been updated.

I saw an odd thing on the wiki with regard to an alleged new mode {$mode iso}. It becomes effective as of FPC version 3.0.2.  I thought a version change at the third digit was bug fixes only.

Well, you are wrong on both accounts (again?)
Because this is in the release notes 2.6..* AND  the FPC 3.0 documentation.
Also: the ISO mode is still experimental in 3.0 and won't be declared otherwise until devel's says so.

I think you may be confused by how many work is done in trunk and fixes, because you are used to seeying only releases by commercial compiler vendors.
The difference : commercial vendors keep development code for their own with a very strict privacy policy regarding testers (of which I used to be one for many years) and the development cycle that is pretty open to all.
« Last Edit: July 06, 2016, 11:54:53 am by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: String Overloading / $LongStrings
« Reply #11 on: July 06, 2016, 12:25:46 pm »

Neither of those is listed in the current online documentation. I assume they are new as of 3.0.0 and the documentation has not been updated.

I saw an odd thing on the wiki with regard to an alleged new mode {$mode iso}. It becomes effective as of FPC version 3.0.2.  I thought a version change at the third digit was bug fixes only.

Well, you are wrong on both accounts (again?)

Here is the online documentation for modeswitch and here is the online documentation for mode. Unless you can find some other online documentation that has them in it, I won't require an apology, just admitting that you were wrong (again?).

« Last Edit: July 06, 2016, 12:28:53 pm by Pasqualish »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: String Overloading / $LongStrings
« Reply #12 on: July 06, 2016, 12:34:36 pm »
@Pasqualish

That something is not present in the official online docs, does not necessary mean the feature is not there. As already mentioned "Also: the ISO mode is still experimental in 3.0 and won't be declared otherwise until devel's says so."

It is even mentioned on wikipedia.
Quote
As of version 2.7.1, Free Pascal implemented basic ISO Pascal mode, though many things such as Get and Put procedure and file buffer variable concept for file handling were still absent.

As of version 3.0.0, ISO Pascal mode is fairly complete, with one remaining bug that's fixed in 3.1.1 afterwards. It has been able to compile standardpascal.org's P5 with no changes.

Thaddy

  • Hero Member
  • *****
  • Posts: 10482
Re: String Overloading / $LongStrings
« Reply #13 on: July 06, 2016, 01:04:41 pm »
Besides: if you generate the documentation from trunk (which takes some effort and knowledge) almost all I explained is already there.
So my remark was justified,

If you want to use cutting edge features, plz note what it takes to use the cutting edge features. Otherwise use the stable release and stick to the features that are advertised in the documentation for the stable release. If you do not know how to use trunk and trunk documentation, basically, there are some muscles somewhere around your face that should be in the relaxed position.

For normal users the on-line documentation should and must reflect the latest stable release.

But feel free to acquire the knowledge to use trunk and its trunk documentation. It is very well documented how to do that ;)
« Last Edit: July 06, 2016, 01:09:57 pm by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: String Overloading / $LongStrings
« Reply #14 on: July 06, 2016, 01:30:20 pm »
@Pasqualish

That something is not present in the official online docs, does not necessary mean the feature is not there.

My comment that it was not present in the online documentation was not representative of a belief that the feature did not exist.


 

TinyPortal © 2005-2018