Lazarus

Free Pascal => General => Topic started by: deadserious on June 15, 2022, 05:23:18 am

Title: RTL Namespaces
Post by: deadserious on June 15, 2022, 05:23:18 am
I'm trying to compile some Delphi units in FPC and running into an issue where the System namespace is unknown.  Units like System.SysUtils, System.DateUtils, System.IOUtils, etc aren't found.  I'm guessing based on the file names in the units/rtl folder you haven't yet namespaced your RTL units in FPC?

How can we tell FPC that System.* should alias to units without System namespace?  Or is there a different RTL I should be pointing to that is already namespaced to match Delphi?
Title: Re: RTL Namespaces
Post by: PascalDragon on June 15, 2022, 09:04:11 am
I'm guessing based on the file names in the units/rtl folder you haven't yet namespaced your RTL units in FPC?

Correct.

How can we tell FPC that System.* should alias to units without System namespace?

You can't.

Or is there a different RTL I should be pointing to that is already namespaced to match Delphi?

There isn't.
Title: Re: RTL Namespaces
Post by: deadserious on June 15, 2022, 09:29:10 am
If I were to take on the effort of renaming RTL units to their namespace, would that be a welcome PR for the team? 

Or is there no interest in Delphi namespace compatibility?
Title: Re: RTL Namespaces
Post by: Thaddy on June 15, 2022, 09:46:25 am
If the dot would be supported in FPC's macro facility, it could come a long way.
Title: Re: RTL Namespaces
Post by: marcov on June 15, 2022, 10:14:35 am
It's a well known issue.  If a change will be made, it will only be made near a major version transition to keep the transition period as short as possible, I will put it on the major version checklist.

However there are probably also certain legacy target concerns, since it possibly eats into their maximum filepath. That discussion still must be had (short term desirability).

(I hope that since you use Delphi, I don't have to tell you how intrusive those changes are)

I simply work the other way, I enter the skip namespace tag in Delphi, and rename all my units on that side.
Title: Re: RTL Namespaces
Post by: PascalDragon on June 15, 2022, 02:19:59 pm
If I were to take on the effort of renaming RTL units to their namespace, would that be a welcome PR for the team?

While we do think about introducing namespaces in the future as marcov said this is not a topic that is simply done, mainly because we also support operating systems that have a 8.3 file name limit. So, no, a PR would not be welcome for this as there are quite some questions to solve.

Or is there no interest in Delphi namespace compatibility?

It won't be fully compatible anyway. E.g. unit Contnrs might just as well be placed in FCL.Contnrs instead of System.Contnrs (I'm not saying that it will be, but it's at least a possibility). And the LCL units on the other hand will very likely use LCL as prefix instead of VCL.
Title: Re: RTL Namespaces
Post by: Thaddy on June 15, 2022, 03:35:50 pm
The latter is already no problem
Code: Pascal  [Select][+][-]
  1. {$macro on}{$define vcl:=lcl}
As I wrote, it is only about the dot....
IOW then you can make prefixes disappear...

I asked Jonas for that macro extension a couple of years ago, but he dismissed that.
Now it bites,  again...
Title: Re: RTL Namespaces
Post by: marcov on June 15, 2022, 03:40:36 pm
It won't be fully compatible anyway. E.g. unit Contnrs might just as well be placed in FCL.Contnrs instead of System.Contnrs (I'm not saying that it will be, but it's at least a possibility). And the LCL units on the other hand will very likely use LCL as prefix instead of VCL.

I surely hope not ;)
Title: Re: RTL Namespaces
Post by: jcmontherock on June 15, 2022, 04:23:43 pm
However, for example, you can use "System.Delete()" to execute a procedure if you already have a "Delete()" procedure in your application. It's a little namespace.
Title: Re: RTL Namespaces
Post by: marcov on June 15, 2022, 04:30:39 pm
System is also the name of the always-there unit.  This has worked since turbo pascal times.
Title: Re: RTL Namespaces
Post by: deadserious on June 16, 2022, 02:48:22 am
I simply work the other way, I enter the skip namespace tag in Delphi, and rename all my units on that side.

Since anonymous methods are now supported, I'm trying to check compatibility with all of my open source libraries. They all already use namespaced units. I'd rather not despecify in my code and instead spend my time contributing to get fpc up to speed.

But if the work will end up not being accepted, it's not going to be worth my time.
Title: Re: RTL Namespaces
Post by: deadserious on June 16, 2022, 02:51:41 am
It won't be fully compatible anyway. E.g. unit Contnrs might just as well be placed in FCL.Contnrs instead of System.Contnrs (I'm not saying that it will be, but it's at least a possibility). And the LCL units on the other hand will very likely use LCL as prefix instead of VCL.

I'm not expecting VCL.* to be compatible with LCL.* lol.

But like units in the RTL should be namespaced to match with Delphi imo.

It sounds like you disagree. It's been that sentiment that has kept me from fpc for so long.
Title: Re: RTL Namespaces
Post by: avra on June 16, 2022, 08:33:40 am
I'm not expecting VCL.* to be compatible with LCL.* lol.

But like units in the RTL should be namespaced to match with Delphi imo.

It sounds like you disagree. It's been that sentiment that has kept me from fpc for so long.
I do not see it as a problem if some ifdefs or macros can jump in and help. It might even be that at the end most of the work is already done and transparent for you. There will never be a 100% compatibility, especially when FPC has to support embedded targets like AVR and run on old ones like DOS. But the expectation to be able to write code that compiles on both Delphi and FPC with little effort remains.
Title: Re: RTL Namespaces
Post by: MarkMLl on June 16, 2022, 09:55:38 am
However, for example, you can use "System.Delete()" to execute a procedure if you already have a "Delete()" procedure in your application. It's a little namespace.

System is also the name of the always-there unit.  This has worked since turbo pascal times.

I'd suggest that any departure from this which breaks compatibility with older FPC versions would be a very bad idea.

If we had to vote between keeping FPC compatible with both itself and older versions of Delphi, versus breaking compatibility in the name of chasing enhancements added to Delphi in its dotage, I'd be firmly on the side of tradition.

MarkMLl
Title: Re: RTL Namespaces
Post by: PascalDragon on June 16, 2022, 11:01:22 am
It won't be fully compatible anyway. E.g. unit Contnrs might just as well be placed in FCL.Contnrs instead of System.Contnrs (I'm not saying that it will be, but it's at least a possibility). And the LCL units on the other hand will very likely use LCL as prefix instead of VCL.

I surely hope not ;)

For units that are part of the FPC distribution it might indeed be less likely that they'll have a different namespace, but I already know that dynamic packages won't be compatible, because there I'll favor maintainability before compatibility.

However, for example, you can use "System.Delete()" to execute a procedure if you already have a "Delete()" procedure in your application. It's a little namespace.

It's the “namespace” provided by the System unit which is automatically used in every unit that isn't the System unit.

I'd suggest that any departure from this which breaks compatibility with older FPC versions would be a very bad idea.

If we had to vote between keeping FPC compatible with both itself and older versions of Delphi, versus breaking compatibility in the name of chasing enhancements added to Delphi in its dotage, I'd be firmly on the side of tradition.

Once units are namespaced (and assuming that their base name stays the same) you can simply use the already existing -FN option to define default namespaces. E.g. -FNSystem with a unit Classes will lead to the compiler looking for both System.Classes and Classes. So you can keep the source the same, but just adjust the compilation parameters.
Title: Re: RTL Namespaces
Post by: marcov on June 16, 2022, 11:04:35 am
Once units are namespaced (and assuming that their base name stays the same) you can simply use the already existing -FN option to define default namespaces. E.g. -FNSystem with a unit Classes will lead to the compiler looking for both System.Classes and Classes. So you can keep the source the same, but just adjust the compilation parameters.

(and that is also what I do on the delphi side. But not everbody does, and in my experience the last 5-8 years the bulk of Delphi users seem to  have moved from the eternal D7 to unicode versions, and pretty new ones even)
Title: Re: RTL Namespaces
Post by: MarkMLl on June 16, 2022, 11:37:16 am
Allowing that this is probably a minority position, but could I highlight a use case and ask whether any change relating to namespaces etc. would cause problems.

For some years I have used a program organisation where a library- perhaps a backend to interface to a specific physical instrument- can be linked either statically (ordinary "uses" clause) or dynamically (run-time dynamic loading of a .so or .dll).

If the linkage is static, the library (e.g. Dso112a) appears to be a unit and functions it exports may be accessed as e.g. Dso112a.MagicNumber().

If the linkage is dynamic, the library is an instance of a class wrapping the .so or .dll, loading the binary and finding entry points at first use. Exported functions/methods may again be accessed as e.g. Dso112a.MagicNumber() where in this case Dso112a is the name of an instance.

Could the introduction of namespaces mess up this convention? If so, is there an elegant workaround?

MarkMLl


Title: Re: RTL Namespaces
Post by: PascalDragon on June 17, 2022, 09:13:37 am
Could the introduction of namespaces mess up this convention?

Why should it? We're talking about introducing namespaces for the RTL, FCL, LCL units, not your own. And the mechanisms for these are already in place since 3.0.0 (namespace support itself) and 3.2.0 (the -FN-parameter).
Title: Re: RTL Namespaces
Post by: MarkMLl on June 17, 2022, 10:09:42 am
Could the introduction of namespaces mess up this convention?

Why should it? We're talking about introducing namespaces for the RTL, FCL, LCL units, not your own. And the mechanisms for these are already in place since 3.0.0 (namespace support itself) and 3.2.0 (the -FN-parameter).

Thanks, I was just being very cautious :-)

MarkMLl
Title: Re: RTL Namespaces
Post by: deadserious on June 17, 2022, 04:41:51 pm
What i think I'll do is build a tool that updates RTL units with namespaces and rebuilds the libraries so that people who wish too have a namespaced rtl can.  It would just require running the tool after updating fpc.
Title: Re: RTL Namespaces
Post by: PascalDragon on June 19, 2022, 01:24:26 pm
So you expect third party providers to cater to both, those that want to use namespaces and those who don't? Also units are provided precompiled in the installers on purpose cause one doesn't have to fiddle with building FPC oneself. Also someone would need to maintain that mechanism.
There's no sense in making things more complicated than they are.
Title: Re: RTL Namespaces
Post by: marcov on June 19, 2022, 01:26:11 pm
I'd be interested in the opposite, a tool to strip namespaces from delphi units.

Title: Re: RTL Namespaces
Post by: Thaddy on June 20, 2022, 02:39:52 pm
I'd be interested in the opposite, a tool to strip namespaces from delphi units.
I have one. It is simple but not complete: does not parse defines inside the uses clause, does not parse implementation yet and is single file.
I will see if I can find it. It dates from the introduction of namespaces in Delphi.
I always wanted to rewrite it with RegExpr, if I remember correctly. (It is old stuff)
Title: Re: RTL Namespaces
Post by: deadserious on June 21, 2022, 08:53:45 pm
So you expect third party providers to cater to both, those that want to use namespaces and those who don't? Also units are provided precompiled in the installers on purpose cause one doesn't have to fiddle with building FPC oneself. Also someone would need to maintain that mechanism.
There's no sense in making things more complicated than they are.

I guess maybe I'll need to provide my own distro then lol.
Title: Re: RTL Namespaces
Post by: PascalDragon on June 22, 2022, 09:07:58 am
I guess maybe I'll need to provide my own distro then lol.

Which you are definitely free to do, but please note that especially once dynamic packages are fully supported this might lead to problems there as well (not to mention that the whole of Lazarus is geared towards what is provided by FPC itself, so you need to keep around both your own as well as a standard distribution to be able to build Lazarus).
Title: Re: RTL Namespaces
Post by: DonaldShimoda on February 22, 2023, 10:10:16 pm
So, there´s a strong decision in no support for this naming. Or any change on this (old) topic? I have a lot of Delphi 10 code to use on FPC for arm. Must put a lot of ifdef? Or theres a more elegant solution?
Title: Re: RTL Namespaces
Post by: marcov on February 23, 2023, 09:03:08 am
No ifdef, just remove all namespaces in the source, and then set the default namespace in the delphi IDE to compensate that, and presto it works on both.
Title: Re: RTL Namespaces
Post by: KodeZwerg on February 23, 2023, 09:38:10 am
In Delphi I do since years....
Code: Pascal  [Select][+][-]
  1. {$IF Defined(DCC) or Defined(VER210) or Defined(VER200) or Defined(VER190) or Defined(VER185) or Defined(VER180) or Defined(VER170) or Defined(VER160) or Defined(VER150) or Defined(VER140) or Defined(VER130) or Defined(VER120) or Defined(VER100) or Defined(VER90) or Defined(VER80)}
  2.   {$DEFINE Delphi} { Delphi }
  3. {$IFEND}
  4.  
  5. {$IF Defined(DELPHI) and Declared(CompilerVersion) and (CompilerVersion >= 23)}
  6.   {$DEFINE NameSpace} { Delphis NameSpace feature (eg Winapi.Windows instead of Windows) }
  7. {$IFEND}
  8.  
  9. {$IF Defined(WIN32) or Defined(WIN64) or Defined(MSWindows)}
  10.   {$DEFINE Windows} { We are on Windows }
  11. {$IFEND Windows}
...via include file

Since I started not that long ago to use also FreePascal my unit header looking like this (exemplary)
Code: Pascal  [Select][+][-]
  1. {$IFDEF DELPHI}
  2.   {$IFDEF NameSpace}
  3.     {$IFDEF Windows}
  4.       Winapi.Windows,
  5.     {$ENDIF Windows}
  6.     System.SysUtils, System.Classes,
  7.     Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
  8.   {$ELSE NameSpace}
  9.     {$IFDEF Windows}
  10.       Windows,
  11.     {$ENDIF Windows}
  12.     SysUtils, Classes,
  13.     Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
  14.   {$ENDIF NameSpace}
  15. {$ENDIF DELPHI}
  16. {$IFDEF FPC}
  17.   {$IFDEF Windows}
  18.     Windows,
  19.   {$ENDIF Windows}
  20.   SysUtils, Classes,
  21.   Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
  22. {$ENDIF FPC}
Title: Re: RTL Namespaces
Post by: DonaldShimoda on February 23, 2023, 02:55:43 pm
No ifdef, just remove all namespaces in the source, and then set the default namespace in the delphi IDE to compensate that, and presto it works on both.

Not so easy. VCl and FMX have sense on delphi. And i prefer to not lose delphi compatibility.

So ifdef is the only way.

Title: Re: RTL Namespaces
Post by: marcov on February 23, 2023, 05:00:29 pm
No ifdef, just remove all namespaces in the source, and then set the default namespace in the delphi IDE to compensate that, and presto it works on both.

Not so easy. VCl and FMX have sense on delphi. And i prefer to not lose delphi compatibility.

For me FMX never made sense on Delphi. btw: I started originally with this scheme because I had XE at home, but XE3 at work, nothing to do with FPC.
Title: Re: RTL Namespaces
Post by: DonaldShimoda on February 23, 2023, 05:16:07 pm
For me FMX never made sense on Delphi.

For you? I don't know which to say to this. We use FMX in several scenarios, in all of them was a very productive library, more an more the latest releases. I understand for you have no sense but what about all the community using delphi and fpc and hoping this to be source code compatible?

Don't get me wrong. I love the work you have done with fpc and lazarus. but I imagine that the objective is that more and more people can use it in the way that is useful to them and in the simplest way possible. Or did I miss something?
Title: Re: RTL Namespaces
Post by: marcov on February 23, 2023, 06:15:52 pm
For me FMX never made sense on Delphi.

For you? I don't know which to say to this.

Yes, the comment (and the Delphi/Lazarus balancing in general) is firmly work related. My hobby work is all Lazarus/FPC ( since about 2017-18 when I converted a few private apps that were very unicode sensitive that I kept in Delphi. My personal private license Delphi was 2009, and it was ageing, badly)

Back to work related, I do have friends that work in businesses that also cater to government and healthcare (rather than just plain manufacturing businesses like we do), and Apple is much more common there. So I realise it is business dependent., but in our business however, Apple is an extreme niche.

And then the Lazarus compatibility (including small UDP/TCP relay servers on RPIs) weighs much more heavy than the small chance that some Apple using consultant will require a FMX route at some point.

All shipped work software currently is Delphi based, though some satellite utils are FPC/Lazarus based. Only last year we first managed to compile major work apps with Lazarus/FPC.
TinyPortal © 2005-2018