[fpc-pascal] Re: How to avoid namespace name clashes after USES ?
terminatorul at gmail.com
Wed Aug 22 12:37:25 CEST 2012
On 08/22/2012 09:57 AM, Marco van de Voort wrote:
> In our previous episode, Marcos Douglas said:
>>> Requiring all functions to be allways fully qualified will make the
>>> most commom case worse than current implementation just to be better
>>> for the uncommon case. This is a step back, not forward...
>> Timothy Madden want this, not me. ;-)
This is necessary in order to guarantee thare can be no conflicts
whatsoever. No matter how many units you want to USE in your program, no
matter what versions of the units.
It is not that bad, python uses that from the start. You must call
os.path.basename(), and there is not way for you to call basename()
directly (well, there is one, but they have taken special measures for
it, so no name conflicts are possible).
But for this to work, I need a way to alias the importing module,
because not all units have a 2-letter name like "os", for example I need
USES 'TrigonometricLibrary' AS 'trig' in 'pas/TrigonometricLibrary.pas'
Of course I will not remove the existing behavior for USES, that is
programmers can still say:
USES charset, matrix, socket;
and they get the behaviour they know.
But if the programmer says:
USES charset AS cs, matrix AS matrix, socket AS socket;
than no symbols are made directly visible, and all of them need their
prefix now (one of cs. or matrix. or socket. ). These prefixes are
chosen by the programmer so thay may not create conflicts, as long as
programmer sticks to the new form of USES.
If both forms of USES are intermixed, like
USES unit1, unit2, charset AS cs, matrix AS matrix, socket AS socket;
than there is a chance that prefix cs will conflict with some unit1.cs
symbol (either now or in a future version of unit1, or unit2). In this
case I think it is appropriate to make the prefix "cs.", introduced by
"charset AS cs", to have precedence over unit1.cs. This is because the
programmer must use the prefix "cs." a lot in the program ((s)he has no
choice, since the prefix is the only way to access simbols from unit
In a corporate environment there will probably be some convention saying
programmers have to use the second USES syntax at all times, to ensure
no future name conflicts are possible.
As I said many Java programmers have been bitten by this problem, and
now they explicitly list every single symbol they need to import from
every package, composing what is known as "imports lists". Which is
surely not fun at all, but it is safe! Most Java IDEs othen try to
automate the generation of such lists, and collapse them when the source
file is opened, because the lists can get very long.
More information about the fpc-pascal