Recent

Author Topic: Private property of a Generic Type  (Read 1063 times)

Okoba

  • Hero Member
  • *****
  • Posts: 533
Private property of a Generic Type
« on: February 26, 2024, 05:39:51 pm »
I have codes like this that worked just find until recent versions, but current Trunk says "unit1.pas(22,5) Error: Identifier idents no member "A""
Why? And what can I do to fix it? I do not want to expose these fields.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses
  6.   unit1;
  7.  
  8. var
  9.   V: TTest<Integer>;
  10. begin
  11.   Inc<Integer>(V);
  12.   ReadLn;
  13. end.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode Delphi}
  4.  
  5. interface
  6.  
  7. type
  8.   TTest<T> = record
  9.   private
  10.     A: T;
  11.   end;
  12.  
  13. procedure Inc<T>(var v: TTest<T>);
  14.  
  15. implementation
  16.  
  17. procedure Inc<T>(var v: TTest<T>);
  18. begin
  19.   V.A += 1; //Error!
  20. end;
  21.  
  22. end.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Private property of a Generic Type
« Reply #1 on: February 26, 2024, 05:51:08 pm »

TRon

  • Hero Member
  • *****
  • Posts: 2518
Re: Private property of a Generic Type
« Reply #2 on: February 26, 2024, 05:57:47 pm »
I assume Okoba meant 3.2.2 ASerge.

Code: [Select]
fpc -B project1.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling project1.pas
Compiling unit1.pas
unit1.pas(10,9) Note: Private field "<record type>.A" is never used
Linking project1
35 lines compiled, 0.0 sec
1 note(s) issued
Note the irony  :)

My most recent trunk is apparently too old as 3.3.1 compiles the project for me as well.

Compilation with brand new trunk:
Code: [Select]
$ fpc-trunk -B project1.pas
Free Pascal Compiler version 3.3.1 [2024/02/26] for x86_64
Copyright (c) 1993-2024 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling project1.pas
Compiling unit1.pas
unit1.pas(10,9) Note: Private field "<record type>.A" is never used
unit1.pas(19,9) Error: Identifier idents no member "A"
project1.pas(14) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /media/ramdisk/fpc/3.3.1/bin/ppcx64 returned an error exitcode
« Last Edit: February 26, 2024, 06:38:33 pm by TRon »

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Private property of a Generic Type
« Reply #3 on: February 27, 2024, 09:14:20 am »
@ASerge I dont know. What I know is until 2024-02-02 it is working but when I get the latest trunk I get this error.

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: Private property of a Generic Type
« Reply #4 on: February 27, 2024, 09:29:37 am »
Yes, they closed the loophole. It's just not clear why you should try to hide everything, it's often not justified((
I used write access to container data, now it's not allowed and I need to add it to the rtl code
https://gitlab.com/freepascal.org/fpc/source/-/issues/40656
https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/593

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Private property of a Generic Type
« Reply #5 on: February 27, 2024, 09:38:35 am »
No not privatizing all properties, it was just a demo to show the problem.
Imagine this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TTest<T> = record
  3.     B: Integer;
  4.     X: String;
  5.   private
  6.     MyPrivateValueToHandle: T;
  7.   end;
  8.  
  9. procedure Inc<T>(var v: TTest<T>);
  10.  
  11. implementation
  12.  
  13. procedure Inc<T>(var v: TTest<T>);
  14. begin
  15.   V.MyPrivateValueToHandle += 1; //Error!
  16. end;
  17.  
  18. end.      

There are values that you don't want outside can handle, or even IDE suggest it in code suggestion.

And it is not consistent and logical that a function has no access to a property in the same unit. Even if we say the type was specialized in the program, the Add function was specialized there too, so both should see each other.
« Last Edit: February 27, 2024, 09:46:10 am by Okoba »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: Private property of a Generic Type
« Reply #6 on: February 27, 2024, 12:31:27 pm »
Yes, they closed the loophole. It's just not clear why you should try to hide everything, it's often not justified((

That "close the loophole" did go to strict on a couple of other constructs too.

It is quite possible that this too, is a bug. I suggest to report it, and wait.

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Private property of a Generic Type
« Reply #7 on: February 27, 2024, 12:48:21 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Private property of a Generic Type
« Reply #8 on: February 27, 2024, 04:06:06 pm »
side note:
This line  V.MyPrivateValueToHandle += 1; //Error!.
makes an assumption that T is an ordinal which we can not yet write as a restriction. Like procedure test<T:ordinal>(); // makes the code safer because T can now only specialize as ordinal.
This kind of type restriction seems on the todo list and has been discussed.
So technically speaking it is currently a bug in your example, the assumption, that is.
I know it is only to demo, but just in case.
« Last Edit: February 27, 2024, 04:09:34 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Private property of a Generic Type
« Reply #9 on: February 27, 2024, 04:10:08 pm »
I know that Thaddy and I am looking forward to the feature, but for now and this sample, the point is accessing private properties form the same unit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Private property of a Generic Type
« Reply #10 on: February 27, 2024, 08:46:59 pm »
So technically speaking it is currently a bug in your example, the assumption, that is.

It's not a bug, because in FPC the most type checking only happens at the specialization time (like with C++) while in Delphi all needs to be fine at the time the generic is defined. FPC's generics are more flexible than Delphi's in that regard.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Private property of a Generic Type
« Reply #11 on: February 27, 2024, 09:31:33 pm »
I wrote about his assumption and both of our wish: <T:ordinal>
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018