In C++, if I have to call a function from another namespace, say foo
, I have two options: adding the using namespace foo;
above my code or specifying the namespace when I perform the call, that is foo::myFunction()
.
In Delphi, is there a construct corresponding to the second alternative?
No, there isn't. If an identifier (such as a function, like myFunction
) is declared in unit foo
, then in a different unit, bar
, say, you cannot use myFunction
without adding foo
to the uses
clause either in the implemantation
or in the interface
section.
Some discussion
Let's say that you have two functions MyFunc
, one in foo
and one in bar
. Let's say you're in foo
. If you don't add
bar to one of the uses
lists, then MyFunc
will refer to foo.MyFunc
, and you cannot access the other function. If you add bar
to one of the uses
lists, then MyFunc
will still refer to the local function foo.MyFunc
, but you can access the other by writing bar.MyFunc
. To reduce the risk of confusion, you could choose always to be explicit and write foo.MyFunc
and bar.MyFunc
, and never only MyFunc
.
A common scenario is this: you got an identifier ident
in both foo
and bar
(they could be very different), and in MyUnit
, you have both foo
and bar
in some uses
list. Then, in MyUnit
, ident
will refer either to foo.ident
or bar.ident
, depending on which unit comes last in the uses clauses. To use the other one, you have to be explicit about the unit (like foo.ident
or bar.ident
). Of course, to reduce the risk of making mistakes, you could always be explicit (e.g. write foo.ident
even if foo
comes last, so that ident
also refers to foo.ident
).
A common mistake is to confuse Windows.TBitmap
with Graphics.TBitmap
. Normally, TBitmap
refers to Graphics.TBitmap
(the Windows
unit usually comes early in uses
clauses). This confusion has been treated at SO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With