[fpc-devel] FPC Macros: Could it be used for unit aliases?

Marcos Douglas B. Santos md at delfire.net
Sun Jun 24 16:49:45 CEST 2018


In another thread[1] Sven Barth explained to me how to use macros
properly following my example before.
However, I think this could be used as a syntax sugar for unit aliases
on the code — not about paths.

Graeme and me have already proposed[2] this years ago. I am against to
include more features in the Language, but in that case looks like
simple because the compiler already has the functionality.

This code looks pretty simple using the classic Pascal:
=== code begin ===
uses
   Windows, Graphics; // both have a type TBitmap
var
  B1: Windows.TBitmap;
  B2: TBitmap;
=== code end ===

There is nothing wrong, of course, but nowadays we have more complex
systems that use more units and that units should be unique in entire
system. So, using namespaces (I know that units are namespaces) turns
the code beautiful without (in theory) unit conflict names. But we
still have classes with the same name. We can use the same approach
above, which is using the unit as a prefix, however this could be
inconvenient because units (nowadays) could have a long name.

Hypothetically, let's suppose that Windows and Graphics unit belongs
to `FPC.RTL.` namespace, a "long name".
Using a syntax sugar to macros, I propose to implement the "as"
keyword in units declarations:

=== code begin ===
uses
   FPC.RTL.Windows as Win,
   FPC.RTL.Graphics as Graph;
var
  B1: Win.TBitmap;
  B2: Graph.TBitmap;
=== code end ===

if I understood right, the compiler "just" need to replace this syntax
to use macros:

=== code begin ===
uses
   {$macro on}
   FPC.RTL.Windows,
   {$define Win := FPC.RTL.Windows}
   FPC.RTL.Graphics as Graph;
   {$define Graph := FPC.RTL.Graphics}
var
  B1: Win.TBitmap;
  B2: Graph.TBitmap;
=== code end ===

In my company we are used to use namespaces like
`Acme.SysFoo.Finances.Billing.Utils.Classes` (just an example).
Depending on the context, I could code `uses ... as Billing;`
Using `Billing` as prefix for all interfaces/classes that has the same
names in others units.

[1] http://lists.freepascal.org/pipermail/fpc-pascal/2018-June/054255.html
[2] http://wiki.freepascal.org/Namespaces#The_.22uses.22_clause

Best regards,
Marcos Douglas

PS. If it might be accepted, please, make it usable in objfpc and
delphi mode. I use the last one because the syntax (eg. generics) is
simpler.



More information about the fpc-devel mailing list