Recent

Author Topic: compilation failure: generic class  (Read 2070 times)

Bagout

  • Guest
compilation failure: generic class
« on: December 22, 2018, 04:28:01 pm »
Hi ! (sorry in advance for my bad English...)

I don't understand why my code doesn't work :

Code: Pascal  [Select][+][-]
  1.   generic TNullable<T> = class
  2.     private
  3.       FValue : T;
  4.     public
  5.       isAssigned : boolean;
  6.       function IsNull: boolean;
  7.       procedure SetValue (newValue: T);
  8.       Property Value : T read FValue write SetValue; default;
  9.     end;
  10.  
  11.  procedure TNullable.SetValue (newValue: T);
  12.  begin
  13.    FValue := newValue;
  14.    isAssigned := true;
  15.  end;
  16.  
  17.  function TNullable.IsNull: boolean;
  18.  begin
  19.    result := not isAssigned;
  20.  end;
  21.  
  22.   type
  23.     TBoolean = specialize TNullable<boolean>;
  24.   Var
  25.     bool: TBoolean;
  26.   begin
  27.     bool := true;
  28.     if bool then writeln('bool is true');
  29.  end.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: compilation failure: generic class
« Reply #1 on: December 22, 2018, 04:47:48 pm »
I made it work in {$mode objfpc} in FPC 3.3.1:
Code: Pascal  [Select][+][-]
  1. Property Value : T read FValue write SetValue;// default;
Property can't be default.

Code: Pascal  [Select][+][-]
  1.   Var
  2.     bool: TBoolean;
  3.   begin
  4.     bool := TBoolean(true);
  5.     if bool= TBoolean(true) then writeln('bool is true');
  6.   end;
This is maybe some compiler issue, since retyped had to be even a nil in past (see https://bugs.freepascal.org/view.php?id=34037).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 18978
  • Glad to be alive.
Re: compilation failure: generic class
« Reply #2 on: December 22, 2018, 05:29:47 pm »
The code indeed does not work in FPC, not even trunk because the default keyword for types other than arrays is not (yet) implemented.
(And also not smartpointers, so you have to create/free your object)
That is a NewPascal feature, not FPC.
Your code should have looked something  like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type
  3.  generic TNullable<T> = class
  4.     private
  5.       FValue : T;
  6.     public
  7.       isAssigned : boolean;
  8.       function IsNull: boolean;
  9.       procedure SetValue (newValue: T);
  10.       Property Value : T read FValue write SetValue;
  11.     end;
  12.  
  13.  procedure TNullable.SetValue (newValue: T);
  14.  begin
  15.    FValue := newValue;
  16.    isAssigned := true;
  17.  end;
  18.  
  19.  function TNullable.IsNull: boolean;
  20.  begin
  21.    result := not isAssigned;
  22.  end;
  23.  
  24.   type
  25.     TBoolean = specialize TNullable<boolean>;
  26.   Var
  27.     bool: TBoolean;
  28.   begin
  29.     bool := TBoolean.Create;
  30.     bool.value := true;
  31.     if bool.value then writeln('bool is true');
  32.     bool.free;
  33.  end.

@Blaazen: that that cast works is only because the first field in the class stores the boolean. I don't think that is what you mean... 8-)
« Last Edit: December 22, 2018, 05:54:59 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: compilation failure: generic class
« Reply #3 on: December 22, 2018, 06:04:28 pm »
I mainly forgot to create the instance and the rest is consequence  :(
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 18978
  • Glad to be alive.
Re: compilation failure: generic class
« Reply #4 on: December 22, 2018, 06:10:21 pm »
If he just needs a nullable Boolean type look here: http://forum.lazarus.freepascal.org/index.php/topic,41144.msg295325.html#msg295325 Much easier.
« Last Edit: December 22, 2018, 06:13:39 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018