Forum > General

unit names

(1/2) > >>

440bx:
Hello,

I would like to name a unit with a different name than its filename.  For instance, in the following code:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$APPTYPE CONSOLE}  program UnitTest; uses  util in 'x01_util'  ; begin  writeln('running');   writethis();   readln;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit util;  interface procedure writethis; implementation procedure writethis;begin  writeln('writethis');end; end.                 The compiler complains bitterly about expecting a different name, specifically:
--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---x01_util.pas(1,10) Error: Illegal unit name: util (expecting UTIL) 
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.

Handoko:

--- 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.
--- End quote ---
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.
--- End quote ---
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:
@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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- util in 'x01_util.pas' // or pp or whatever extension used 
edit: more precise link

440bx:
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.

TRon:
It seem to be as clear as a whistle....


--- Code: Bash  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$> fpc -B test.pas Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64Copyright (c) 1993-2021 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling test.pasCompiling testunit.pastestunit.pas(1,15) Error: Illegal unit name: CONFUCIUS (expecting CONFUCIUS)testunit.pas(10,1) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation aborted 
.. working like a charm   ;D

Navigation

[0] Message Index

[#] Next page

Go to full version