Recent

Author Topic: unit names  (Read 953 times)

440bx

  • Hero Member
  • *****
  • Posts: 4335
unit names
« on: May 14, 2024, 07:46:53 am »
Hello,

I would like to name a unit with a different name than its filename.  For instance, in the following code:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2.  
  3.  
  4. program UnitTest;
  5.  
  6. uses
  7.   util in 'x01_util'
  8.   ;
  9.  
  10. begin
  11.   writeln('running');
  12.  
  13.   writethis();
  14.  
  15.   readln;
  16. end.                        
I would like to have the unit named "util" reside in a file named "x01_util".

when I create the file named "x01_util" with the following code:
Code: Pascal  [Select][+][-]
  1. unit util;
  2.  
  3.  
  4. interface
  5.  
  6. procedure writethis;
  7.  
  8. implementation
  9.  
  10. procedure writethis;
  11. begin
  12.   writeln('writethis');
  13. end;
  14.  
  15. end.                
The compiler complains bitterly about expecting a different name, specifically:
Code: Text  [Select][+][-]
  1. x01_util.pas(1,10) Error: Illegal unit name: util (expecting UTIL)
  2.  

The question is: is it possible to have a unit named, e.g, "util" in a file named "x01_util" and if yes, how ?

Thank you for your help.

On a different note, the error message emitted by the compiler isn't particularly helpful.  The compiler complaining that it expected the very unit name it considers illegal doesn't make much sense.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5248
  • My goal: build my own game engine using Lazarus
Re: unit names
« Reply #1 on: May 14, 2024, 08:04:24 am »
Quote
A unit is defined with the unit keyword followed by the unit-identifier. The unit-identifier (in the following example the unit's name is “minimalunit”) should match the filename it is written in.
Source: https://wiki.lazarus.freepascal.org/Unit#Format

Quote
The unit heading specifies the unit's name. It consists of the reserved word unit, followed by a valid identifier, followed by a semicolon. For applications developed using Embarcadero tools, the identifier must match the unit file name.
Source: https://docwiki.embarcadero.com/RADStudio/Sydney/en/Programs_and_Units_(Delphi)

Those are what I found in Free Pascal and Delphi documentations. So if you need to name the file differently, you can try to submit a feature request but you should provide good reasons to convince developers to accept it.

TRon

  • Hero Member
  • *****
  • Posts: 2897
Re: unit names
« Reply #2 on: May 14, 2024, 08:32:33 am »
@handoko:
It exists since stone age  :).

I agree can't find it so quick in FPC docs but here on Delphi it can be found

@440bx:
try
Code: Pascal  [Select][+][-]
  1.  util in 'x01_util.pas' // or pp or whatever extension used
  2.  

edit: more precise link
« Last Edit: May 14, 2024, 08:40:07 am by TRon »

440bx

  • Hero Member
  • *****
  • Posts: 4335
Re: unit names
« Reply #3 on: May 14, 2024, 08:51:56 am »
Thank you Handoko and TRon.

I suspected that it wasn't possible and I did look in the FPC documentation but, did not find any statement I considered conclusive on the matter.  Therefore I figured I'd ask, just in case.

Thank you both again.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

TRon

  • Hero Member
  • *****
  • Posts: 2897
Re: unit names
« Reply #4 on: May 14, 2024, 02:20:00 pm »
It seem to be as clear as a whistle....

Code: Bash  [Select][+][-]
  1. $> fpc -B test.pas
  2. Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling test.pas
  6. Compiling testunit.pas
  7. testunit.pas(1,15) Error: Illegal unit name: CONFUCIUS (expecting CONFUCIUS)
  8. testunit.pas(10,1) Fatal: There were 1 errors compiling module, stopping
  9. Fatal: Compilation aborted
  10.  

.. working like a charm   ;D

PascalDragon

  • Hero Member
  • *****
  • Posts: 5585
  • Compiler Developer
Re: unit names
« Reply #5 on: May 14, 2024, 10:36:21 pm »
The question is: is it possible to have a unit named, e.g, "util" in a file named "x01_util" and if yes, how ?

The answer to your question lies in FPC's help output:

Quote from: FPC help output
-Un        Do not check where the unit name matches the file name

Please note that the in-clause also specifies a path which is (to my surprise I admit) relative to the current working directory, not the directory of the program file.

440bx

  • Hero Member
  • *****
  • Posts: 4335
Re: unit names
« Reply #6 on: May 15, 2024, 12:45:38 am »
Thank you PascalDragon.  I'll give that a try.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018