Recent

Author Topic: unit MyNamespace.MyUnit in file MyUnit.pas  (Read 903 times)

daniel_sap

  • Full Member
  • ***
  • Posts: 106
unit MyNamespace.MyUnit in file MyUnit.pas
« on: October 02, 2025, 10:52:12 am »
Hi
I usually name my units like this
unit MyNamespace.MyUnit;
and the file name is MyNamespace.MyUnit.pas

But was wondering if there is a way to use the file name without the MyNamespace infront, like this
file name MyUnit.pas
unit MyNamespace.MyUnit;

And even better, if I can add only one search path
Source for example and inside it if there is MyNamespace folder to search in it for the unit. So, there will be no need to add all my subfolders to search path.

I tried to achieve it, but not possible

My Lazarus FPC versions are
Lazarus 4.0RC1 (rev t-fixes-4-58-g43d904ed50) FPC 3.2.2 x86_64-win64-win32/win64

May be there is something implemented in the latest development

I found this wiki where there are some ideas but not sure if there is something implemented
https://wiki.lazarus.freepascal.org/Namespaces#Suggestion_3_-_directory_layout

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #1 on: October 02, 2025, 10:58:07 am »
Yes, you can compile - compile yourself - trunk/main fpc with namespaced units and that will resolve it, just like Delphi does.
This is not possible - and will not be possible - in 3.2.2 nor in 3.2.4 RC.

Instructions are here, but be warned: create a separate install for it:
https://wiki.freepascal.org/FPC_Dotted_RTL#:~:text=This%20is%20a%20set%20of%20units%20from%20RTL%2FPackages,been%20used%20to%20make%20the%20names%20more%20consistent.
« Last Edit: October 02, 2025, 11:01:53 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

daniel_sap

  • Full Member
  • ***
  • Posts: 106
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #2 on: October 02, 2025, 11:08:27 am »
Yes, you can compile - compile yourself - trunk/main fpc with namespaced units and that will resolve it, just like Delphi does.
This is not possible - and will not be possible - in 3.2.2 nor in 3.2.4 RC.

Instructions are here, but be warned: create a separate install for it:
https://wiki.freepascal.org/FPC_Dotted_RTL#:~:text=This%20is%20a%20set%20of%20units%20from%20RTL%2FPackages,been%20used%20to%20make%20the%20names%20more%20consistent.

Thank you, Thaddy

For now I think I will use the old way in the packages which are in public repos until the official release
And in my private projects I can use the new stuff

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #3 on: October 02, 2025, 11:24:33 am »
That is a sound conclusion.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12536
  • FPC developer.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #4 on: October 02, 2025, 12:19:54 pm »
May be there is something implemented in the latest development

I found this wiki where there are some ideas but not sure if there is something implemented
https://wiki.lazarus.freepascal.org/Namespaces#Suggestion_3_-_directory_layout

No, the above suggestion is a loose 3rd party remark, and not one that shows intimate familiarity with how the Pascal unit system works. The remark is not from one of the language developers, and will be very hard to implement, with many issues for backwards compatibility, so it is not realistic to expect this to be ever implemented.

FPC, like Delphi do have something opposite, the namepace path, which allows you to use

Code: [Select]
uses myunit;

in your code

while the file on disk is  "mynamespace.myunit"   by passing the -FNmynamespace parameter to the compiler. see also https://wiki.freepascal.org/FPC_New_Features_3.2.0#Support_for_Default_Namespaces 


Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #5 on: October 02, 2025, 01:40:19 pm »
@Marcov

I think - at least I interpreted it that way - he is referring to how namespaces work in Delphi, i.e. the compiler and rtl compiled for namespaced units.
For that to work correctly you need 3.3.1 or higher and the rtl compiled for namespaces.
That does not mean you can not leave out the namespace (namespaces.lst).
That is 2024/25 and not 2018. It is also related to delphi unicode.
https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_Unicode_RTL
and you need to compile the rtl for support.

You are correct in that 3th party code could already be used with namespaces since 3.2.0. but that is not quite the same.
« Last Edit: October 02, 2025, 01:47:55 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

daniel_sap

  • Full Member
  • ***
  • Posts: 106
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #6 on: October 02, 2025, 04:40:08 pm »
May be there is something implemented in the latest development

I found this wiki where there are some ideas but not sure if there is something implemented
https://wiki.lazarus.freepascal.org/Namespaces#Suggestion_3_-_directory_layout

No, the above suggestion is a loose 3rd party remark, and not one that shows intimate familiarity with how the Pascal unit system works. The remark is not from one of the language developers, and will be very hard to implement, with many issues for backwards compatibility, so it is not realistic to expect this to be ever implemented.


Thank you, marcov
for explaining about the https://wiki.lazarus.freepascal.org/Namespaces#Suggestion_3_-_directory_layout
I just found this document and it looked a big one, not sure if there is something inside that is valuable to me.

I can continue using the namespace in the file name,
but now I have a lot of files starting with same text.
For example 20 files starting with MyNamespace.xxx which is not a big problem I can say
but for me it will be much nicer to have the shorter names and easy to read an search these names.

I was thinking that the namespace will be another level of grouping the units (if we look more abstract on the code).
Now they are grouped by folder(tree) structure. (Or lets say every file is Tagged as part of folder. Like "folderspace" lets say)
And now it comes another grouping - namespace, and it doesn't have anything to do with the filesystem (filename)
Later the programing world can come up with third Tag - for example some special space. It will have nothing in common with filesystem too.
So, this was what I'm asking - will the namespace will be removed from the file name.

Nicole

  • Hero Member
  • *****
  • Posts: 1271
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #7 on: October 02, 2025, 05:51:14 pm »
Long time ago, I checked this and found: no.
Not sure about the reasons any more. It was the better way not to do.

I use an underscore.
So my unit is
unit myUnitName
and the file is
myUnitName_

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #8 on: October 02, 2025, 06:05:03 pm »
That is nonsense: the way it is implemented is delphi compatible, you only need to build for it.
Otherwise it would not work in Delphi either.
dotted names are supported since 3.2.0 and a full dotted rtl is supported in trunk.
(Although I just encountered a bug during building)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1566
    • Lebeau Software
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #9 on: October 02, 2025, 07:01:29 pm »
I think - at least I interpreted it that way - he is referring to how namespaces work in Delphi, i.e. the compiler and rtl compiled for namespaced units.

Be careful with terminolgy. In Delphi, Namespaces and Unit Scope Names are two completely different things, even though they use similar syntax in the code.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #10 on: October 02, 2025, 07:53:24 pm »
I think - at least I interpreted it that way - he is referring to how namespaces work in Delphi, i.e. the compiler and rtl compiled for namespaced units.

Be careful with terminolgy. In Delphi, Namespaces and Unit Scope Names are two completely different things, even though they use similar syntax in the code.
I am aware of that. I do not mean unit scope here. That exists much longer.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12536
  • FPC developer.
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #11 on: October 02, 2025, 08:54:09 pm »
I can continue using the namespace in the file name,
but now I have a lot of files starting with same text.
For example 20 files starting with MyNamespace.xxx which is not a big problem I can say
but for me it will be much nicer to have the shorter names and easy to read an search these names.

As said, it is not really likely that this javification is ever happening, simply because it is not compatible with how the unit system currently works. Moreover it would be a delphi compatibility breach. So that are two reasons why it will probably never happen. (never say never, since Delphi devels have done strange things).

I myself removed all namespace usage from my Delphi codes (so simply "sysutils" and "windows " and "stdctrls" ) and removed all prefixes. In the Delphi projects I configured the namespaces.

Free Pascal has some work done on namespaced units, but unfortunately it is hard linked to other features. For me it creates more problems than the minor added delphi compatibility solves (as my string routines work fine 1-byte and with UTF-8 enabled in the manifest).


daniel_sap

  • Full Member
  • ***
  • Posts: 106
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #12 on: October 06, 2025, 11:18:39 am »
But one of the ideas of the namespace, I think, is to have multiple units with same name.
If we have 2 units
SomeUnit.pas -> namespace SomeNamespace1
SomeUnit.pas -> namespace SomeNamespace2

we can specify the correct one in the uses clause
Code: Pascal  [Select][+][-]
  1. uses
  2.   SomeNamespace2.SomeUnit;
  3.  

So, in this case something like this will be enough
Code: Pascal  [Select][+][-]
  1. unit SomeUnit;
  2. namespace MyNamespace2;
  3.  

Cause it is always possible 2 packages(may be 3rd party) to have some unit files with same names.
Or one of your units to have the same name as file in 3rd party package. (so you don't have to rename your file)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6195
  • Compiler Developer
Re: unit MyNamespace.MyUnit in file MyUnit.pas
« Reply #13 on: October 06, 2025, 02:22:23 pm »
But was wondering if there is a way to use the file name without the MyNamespace infront, like this
file name MyUnit.pas
unit MyNamespace.MyUnit;

No, there is not and there will not be, that's simply not how dotted unit names are designed to work and there is no desire to change that. The compiler needs to be able to find the file that the unit specifies and for that it needs to have the full filename on the file system as paths themselves are not part of any namespacing structure in FPC (and Delphi) and will not be.

 

TinyPortal © 2005-2018