Recent

Author Topic: how to cause the compiler to refuse a var declaration ?  (Read 3895 times)

Kays

  • Sr. Member
  • ****
  • Posts: 423
  • Whasup!?
    • KaiBurghardt.de
Re: How to cause the compiler to refuse a var declaration?
« Reply #15 on: January 09, 2022, 02:37:14 pm »
The main reason for those two are to interface with external code and where you do not access or change that record yourself, but must be able to pass it back to some other routine  in the external code.
FYI, the ISO standard 10206 “Extended Pascal” defines the keyword restricted for that:
Code: Pascal  [Select][+][-]
  1. program restrictedDemo;
  2. var
  3.         key: restricted integer;
  4. begin
  5.         key := 42; { ↯ assigning a restricted value isn’t allowed }
  6. end.
This will fail to compile. Obviously you’ll use that in a module and selectively export identifiers so only the restricted type “alias” is “usable” from outside.
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: how to cause the compiler to refuse a var declaration ?
« Reply #16 on: January 09, 2022, 03:25:48 pm »
Yes, I knew that, but 440bx does not use ExtendedPascal mode.
The ISO modes can not be mixed with unit supporting modes.

But maybe we can ask Florian to add restricted ? Because in{$mode extendedpascal} it is not supported yet.
« Last Edit: January 09, 2022, 03:32:14 pm by Thaddy »
Путин преступник. Россияне дезинформированы.

440bx

  • Hero Member
  • *****
  • Posts: 2765
Re: how to cause the compiler to refuse a var declaration ?
« Reply #17 on: January 09, 2022, 03:46:37 pm »
Thanks for the detailed explanation. In my opinion the following code should fix the issue:
No... thank you for your efforts to find a solution.

What you suggested works but I cannot use it because the test is dependent on the variable name.  If the variable name is different then that test won't work.  I need to prevent the use of the empty record type regardless of what the variable name is. 

I still think  PTR_TO_OPAQUE_TYPE = type pointer; is the (a) solution.
<snip>
The above should do in most cases, since it is also a distinct type.
That will work because the pointer is no longer pointing to an empty record type.

I was hoping there was a way to declare the pointer as being a pointer to an empty record type because that makes it easier to keep track of record layouts that I still need to figure out, e.g, have a { TODO } between the "record" and "end;".

@Kays,

As Thaddy mentioned, I cannot use "restricted" because I'm not using the "extended pascal" mode.

I'll just declare the pointer as a plain pointer instead of as a pointer to an empty record and put a { TODO } comment next to it.

Thank you all for your help.
« Last Edit: January 09, 2022, 03:48:34 pm by 440bx »
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: how to cause the compiler to refuse a var declaration ?
« Reply #18 on: January 09, 2022, 04:49:40 pm »
That will work because the pointer is no longer pointing to an empty record type.
It will work. By typing it, the usability is better than a raw pointer, e.g. type checking.
The empty record should not be accessible after all and this comes close.

I will ask Florian to implement restricted ( it is missing in the ExtendedPascal implementation, afaik) and maybe it can be extended to the object pascal modes. In general that would be a very useful feature and imho in such cases as you present better than my Opaque additions to system.
Путин преступник. Россияне дезинформированы.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4014
  • Compiler Developer
Re: how to cause the compiler to refuse a var declaration ?
« Reply #19 on: January 10, 2022, 02:00:33 pm »
Everyone, thank you for all the suggestions and thank you @PascalDragon for a unambiguous answer even if it wasn't what I wanted to read.

You're welcome. Though I'm quite surprised that all others misinterpreted your question... ::)

That will work because the pointer is no longer pointing to an empty record type.
It will work. By typing it, the usability is better than a raw pointer, e.g. type checking.
The empty record should not be accessible after all and this comes close.

I will ask Florian to implement restricted ( it is missing in the ExtendedPascal implementation, afaik) and maybe it can be extended to the object pascal modes. In general that would be a very useful feature and imho in such cases as you present better than my Opaque additions to system.

restricted alone is not enough. As Kays wrote it depends upon the whole module concept of ISO Extended Pascal. And right now FPC's ISO Extended Pascal mode is too much in its infancy for something like that and other things are much more important (like finally releasing 3.2.4).

 

TinyPortal © 2005-2018