Recent

Author Topic: [SOLVED]generics and short strings  (Read 574 times)

avk

  • Sr. Member
  • ****
  • Posts: 332
    • my self-education project
[SOLVED]generics and short strings
« on: November 27, 2020, 01:04:37 pm »
Hi!

Stumbled upon such a quirk, this simple test:
Code: Pascal  [Select][+][-]
  1. program test;
  2. {$mode objfpc}
  3. uses
  4.   Nullable;
  5. type
  6.   TStr8 = string[8];
  7. var
  8.   //s: specialize TNullable<string>; //this compiles
  9.   s: specialize TNullable<TStr8>;
  10. begin
  11.   WriteLn(s.ValueOrDefault);
  12. end.
  13.  
won't compile:
Code: Text  [Select][+][-]
  1. Hint: (11030) Start of reading config file /.../fpc/bin/x86_64-linux/fpc.cfg
  2. Hint: (11031) End of reading config file /.../fpc/bin/x86_64-linux/fpc.cfg
  3. Free Pascal Compiler version 3.3.1-r47607 [2020/11/27] for x86_64
  4. Copyright (c) 1993-2020 by Florian Klaempfl and others
  5. (1002) Target OS: Linux for x86-64
  6. (3104) Compiling test.lpr
  7. /.../fpc/units/x86_64-linux/rtl-objpas/nullable.ppu:nullable.pp(43,48) Error: (3084) Impossible operator overload
  8. /.../fpc/units/x86_64-linux/rtl-objpas/nullable.ppu:nullable.pp(45,43) Error: (3084) Impossible operator overload
  9. test.lpr(13) Fatal: (10026) There were 2 errors compiling module, stopping
  10. Fatal: (1018) Compilation aborted
  11. Error: /.../fpc/bin/x86_64-linux/ppcx64 returned an error exitcode
  12.  

Looks like a bug or am I missing something?
« Last Edit: November 29, 2020, 06:16:51 pm by avk »

bytebites

  • Sr. Member
  • ****
  • Posts: 369
Re: generics and short strings
« Reply #1 on: November 27, 2020, 01:12:22 pm »
Unable to find file "nullable.pp".

avk

  • Sr. Member
  • ****
  • Posts: 332
    • my self-education project
Re: generics and short strings
« Reply #2 on: November 27, 2020, 01:29:28 pm »
I'm sorry. FPC-3.3.1, you can see it in the compiler's message.

Peter H

  • Full Member
  • ***
  • Posts: 120
Re: generics and short strings
« Reply #3 on: November 27, 2020, 05:33:36 pm »

avk

  • Sr. Member
  • ****
  • Posts: 332
    • my self-education project
Re: generics and short strings
« Reply #4 on: November 27, 2020, 05:37:00 pm »
Thank you, but it seems it's not quite the same.
« Last Edit: November 27, 2020, 05:48:38 pm by avk »

Peter H

  • Full Member
  • ***
  • Posts: 120
Re: generics and short strings
« Reply #5 on: November 27, 2020, 08:07:39 pm »
I am not skilled with freepascal or delphi, but I was playing around with this and tried to fix it or find the problem.
So I  changed it:
Code: Pascal  [Select][+][-]
  1.  var
  2.   s: specialize TNullable<string[8]>;
And got this error message:
Code: Pascal  [Select][+][-]
  1. project1.lpr(12,36) Error: Type parameters of specializations of generics cannot reference the currently specialized type
Then I looked it up in Google.

If it is not the same, forget it I do not pretend to understand it fully.  ;)

ASerge

  • Hero Member
  • *****
  • Posts: 1711
Re: generics and short strings
« Reply #6 on: November 27, 2020, 08:28:57 pm »
Unable to find file "nullable.pp".
Example without nullable.pp
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$MODESWITCH ADVANCEDRECORDS}
  3. type
  4.   generic TTest<T> = record
  5.   private
  6.     type TTestType = specialize TTest<T>;
  7.   public
  8.     class operator := (AValue: TTestType): T;
  9.   end;
  10.  
  11. class operator TTest. := (AValue: TTestType): T;
  12. begin
  13.   Result := AValue;
  14. end;
  15.  
  16. type
  17.   TT = string[255]; // this compiles
  18.   //TT = string[254]; // this not
  19. var
  20.   V: specialize TTest<TT>;
  21. begin
  22. end.

Peter H

  • Full Member
  • ***
  • Posts: 120
Re: generics and short strings
« Reply #7 on: November 27, 2020, 08:39:51 pm »
Code: Pascal  [Select][+][-]
  1. program test;
  2. {$mode objfpc}
  3. uses
  4.   Nullable;
  5. type
  6.   //   TStr8 = string[254];   // Doesnt compile
  7.   TStr8 = string[255];        // Compiles
  8. var
  9.   s: specialize TNullable<TStr8>;
  10. begin
  11.   WriteLn(s.ValueOrDefault);
  12. end.

Funny  ;) I never expected this.
I think, if the type is given as TStr8 the compiler detects a problem at a deeper level, when it translates the abstract type definition into real machine code
and if the type is given as string[xyz] it finds the problem sooner -and gives a better error message-, but in both cases it is the same problem.
In any case it is a compiler bug.
« Last Edit: November 27, 2020, 11:27:55 pm by Peter H »

avk

  • Sr. Member
  • ****
  • Posts: 332
    • my self-education project
Re: generics and short strings
« Reply #8 on: November 28, 2020, 09:35:53 am »
IMHO when you define
Code: Pascal  [Select][+][-]
  1. var
  2.   MyVar: TMyGeneric<string[254]>
  3.  
you are passing an anonymous type as a generic parameter, which is a syntax violation according to the documentation. The report you pointed out mostly complains that the compiler does not handle this case properly.

Eventually I reported a bug.

 

TinyPortal © 2005-2018