[fpc-pascal] Ideas for namespace implementation

Graeme Geldenhuys graemeg.lists at gmail.com
Sun Jul 25 19:45:49 CEST 2010


Hi,

As I stated in another thread, namespaces are not perfect, but they do
work, and they do greatly reduce the chances for unitname or classname
conflicts. Other languages and frameworks (Java, .NET, C++ etc)
already showed this.

How visible is the unit name conflict problem?  Just think, how many
projects are there that use the unit names (or would like to use these
common names):
  constants.pas
  utils.pas
  etc...

Extremely common names, and are really good names for what they
contain, so why can't we use them in our projects? Namespaces will
resolve this problem. What we need to discuss, is how to implement
namespace support in FPC.

Some ideas I can think of... please list your own if you have some:

1)  Follow the "dotted" notation for unit names as implemented in Delphi.
   Cons:
     - They force rather long unit names, which is frowned upon in the Pascal
       community (for whatever reason).

   Ambiguities couldn't appear, but they are rather easy to overcome
   with a simple language rule. Object Pascal already have many language
   rules, this will just be a new one.
   See the following Mantis report for details:
      http://bugs.freepascal.org/view.php?id=14439


2) Another idea is in a way similar to how compiler defines work. This
idea tries
    to resolve the problem in (1) with long unit names and with least amount of
    resistance to existing code. A namespace can be applied
    to a single unit, or to a group of units (think of it as a
"package"). To get this to
    work, we will have to introduce a new keyword and a new compiler parameter.
    Here follows examples of both.

    // per unit
   unit utils namespace companyxyz;
   uses
     ...
   end.

    ... or applied at compile time to all units being compiled.

    // our sample unit - as normal, nothing new
    unit utils;
    uses
      ...
    end.

    $ fpc -namespace=companyxyz utils.pas constants.pas ...

    ... if a conflict occurs, then you reference the unit with the
full namespace.
    eg:

      uses
         companyxyz.utils;  // because unit names must be valid
identifier and thus,
                                      // can't contain dots, when the
compiler sees this, it knows
                                      // companyxyz. is a namespace reference.

    Pros:
      - No long unit names are required.
      - We can use all the easy unit names for projects. eg:
utils.pas, constans.pas etc.
      - Can be applied to existing code without any code changes, simply by
        adding a new namespace compiler parameter when compiling that existing
        code. Thus, FPC's RTL and FCL, or fpGUI or Lazarus etc could
apply it too, to
        their makefiles.
      - referencing via a namespace is only needed when a conflict occurs, other
        than that, units are used as normal.
      - If no namespace is specified, then it automatically falls
under the "global"
        namespace - maybe using the namespace value "global" and will be similar
        to all units containing the following:
        eg:
            unit myunit namespace global;
            ...
            end.

            ... or being compiled with 'fpc -namespace=global <units>'


3)  Automatically add the directory where the unit is located, as the
namespace. Similar to what Java does.



I'm sure given more time, I could come up with some more ideas, but I
rather like (2) because it could be applied to all existing code,
without any code changes required.



-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-pascal mailing list