[fpc-pascal] How to avoid namespace name clashes after USES ?

Marcos Douglas md at delfire.net
Tue Aug 21 17:06:11 CEST 2012


On Mon, Aug 20, 2012 at 1:15 PM, Timothy Madden <terminatorul at gmail.com> wrote:
> Hello
>
> As the language is currently specified, an identifier is searched in,
> and used from, the last unit in the USES clause that exposes that
> identifier.
>
> As some might know already, this exposes the user to future
> compatibility problems, stemming from the fact that the USES clause
> imports and "open namespace" into the user program.
>
> Imagine my perfectly tested scientific computations program uses two
> third-party units: Math and Trig. Currently only the Math unit exposes a
> log() function that computes the base e (that Euler number) logarithm of
> the given argument. All is so good and nice and I get promoted or I get
> to another project or to another company and leave the code on the
> repository.
>
> An year later a new version of Trig unit is available, the company
> promptly upgrades it and after some time someone else needs to make some
> changes to, or just re-compile for porting or other resons, the initial
> scietific computations program.
>
> Now Trig also exposes a new log() function, which computes the base 10
> logarithm for its given argument. Now Trig appears last in the USES
> list, after Math.
>
> With no error messages, or even with no changes to the program since 1
> and a half year in the repository, the scientific calculations are now
> all blown up, and program outputs only errors at runtime. The maintainer
> now curses and chases me for having the nerve to leave a program
> known as working in such a horrible mess in the repository.
>
> Of course this is a particular case, and in the general case it is most
> likely I will get some compilation error, if a unit that appears later
> in the USES clause suddenly now defines a previous symbol, that I make
> use of in my program. But still, if the program never changes (on
> git/svn/cvs), it should still compile later, as it stands...
>
> This story is inspired from a real case when *lots* of user code
> suddenly stopped compiling after JDK 1.2 was released, with a new List
> class, which users had to import from a different package until JDK 1.1.
> In this case is wasn't even a third-party library that triggered
> conflicts in existing code, it was the system run-time library. Since
> that story most Java programmers prefer to import and enumerate each and
> every class they use explicitly, instead of importing the package that
> provides the classes (I believe there is no equivalent feature in Pascal
> that imports only one symbol from a unit).
>
> Is there any form of proposal or some alternative to the USES clause,
> that will keep all the imported symbol names qualified by some namespace
> name or by some prefix I choose ?
>
> Something like:
>         import System into sys, Math, Trigonometric into trig;
> and then I would need to use trig.actg() as the only possible way to
> refer to actg() function in the Trigonometric unit, and to use
> Math.log() for logarithm ?
>
> Yes, I know I can qualify names even now if I so choose, but I was
> thinking for a way to ensure that qualified names are the only
> possibility, and a way that will allow me some shorter qualification,
> because with the current qualified names, the qualified name can be
> really long.
>
> Thank you,
> Timothy Madden

I feel your pain.
A long time ago this topic was talked here, on the list, and many
ideas were proposed... but none was accepted or because no one will
implement it.
I proposed this sintaxe:
uses my_long_unit_name as my;
begin
  my.proc();
end

Simple, clean and easy. But I do not know how implement this on the compiler.

IMHO, without changing the language, only the compiler could use
"simple names". All others  third-party library -- even Lazarus --
should use prefixes in ALL identifiers in the unit, unfortunately.

Marcos Douglas



More information about the fpc-pascal mailing list