Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-10

Packages and units with the same name

12 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marcos Douglas B. Santos

Hi,

Imagine that we have two packages and each one has a global.pas unit like this:

BasePack
/pkg
basepack.lpk
/src
global.pas

SmartPack
/pkg
smartpack.lpk
/src
global.pas

Both packages have the same configuration:
Other unit files (-Fu) = ../src

But if SmartPack uses BasePack the compiler says that we have two versions of global.pas

My question is:
How can I put some units in a package but in a way that these units would be invisible in others packages?

I thought that only units included in package would be visible outside (other programs/packages) but I realized that doesn't matter if were included or not, the visibility is defined only by path (-Fu).

Best regards,
Marcos Douglas

Reply by email

Mattias Gaertner
to Marcos Douglas B. Santos

On Fri, 13 Oct 2017 15:42:27 -0300
"Marcos Douglas B. Santos via Lazarus"

[...] I thought that only units included in package would be visible outside (other programs/packages) but I realized that doesn't matter if were included or not, the visibility is defined only by path (-Fu).

Correct. Unit names must be unique in a program.

Mattias

Reply by email

Marcos Douglas B. Santos
to Mattias Gaertner
Mattias Gaertner wrote 10 lines
On Fri, 13 Oct 2017 15:42:27 -0300 "Marcos Douglas B. Santos via Lazarus" wrote: >[...] > I thought that only units included in package would be visible outside > (other programs/packages) but I realized that doesn't matter if were > included or not, the visibility is defined only by path (-Fu). Correct. Unit names must be unique in a program.

Right. But when we use 3rd packages, we don't control which names they will use to name their units.
Moreover, I believe each package should work only in a bounded context. Its developers should not named their files considering that these names could be used in a bigger context. Don't you agree?

IMHO, would be better if packages may have just some "exported units". What do you think?

Best regards,
Marcos Douglas

Reply by email

Mattias Gaertner
to Marcos Douglas B. Santos

On Fri, 13 Oct 2017 16:32:37 -0300
"Marcos Douglas B. Santos via Lazarus"

[...]Its developers should not named their files considering that these names could be used in a bigger context. Don't you agree?

IMO reusable code should be named properly for a bigger context.

IMHO, would be better if packages may have just some "exported units". What do you think?

If you mean Lazarus packages: FPC has only one type of unit, so Lazarus can't mark units private.
Of course you can write a library. Then all unit names are hidden. But then you loose a lot of comfort.
You might want to help FPC with namespaces.

Mattias

Reply by email

Sven Barth
to Marcos Douglas B. Santos: On Fri, Oct 13, 2017 at 4:16 PM, Mattias Gaertner via Lazarus Right. But when we use 3rd…
Marcos Douglas B. Santos wrote 21 lines
On Fri, Oct 13, 2017 at 4:16 PM, Mattias Gaertner via Lazarus wrote: > On Fri, 13 Oct 2017 15:42:27 -0300 > "Marcos Douglas B. Santos via Lazarus" > wrote: > >> [...] >> I thought that only units included in package would be visible outside >> (other programs/packages) but I realized that doesn't matter if were >> included or not, the visibility is defined only by path (-Fu). > > Correct. Unit names must be unique in a program. Right. But when we use 3rd packages, we don't control which names they will use to name their units. Moreover, I believe each package should work only in a bounded context. Its developers should not named their files considering that these names could be used in a bigger context. Don't you agree? IMHO, would be better if packages may have just some "exported units". What do you think?

It doesn't matter. Any symbols in a unit are mangled only locally to the unit and there *can not* be two or more symbols with the same name in one program. What you ask for is simply not possible.

Regards,
Sven

Reply by email

Marcos Douglas B. Santos
to Sven Barth
Sven Barth wrote 9 lines
On 13.10.2017 21:32, Marcos Douglas B. Santos via Lazarus wrote: > [...] > > IMHO, would be better if packages may have just some "exported units". > What do you think? It doesn't matter. Any symbols in a unit are mangled only locally to the unit and there *can not* be two or more symbols with the same name in one program. What you ask for is simply not possible.

Is it not possible *today* or never?
I don't know how the compiler works, but try to understand me: I would like to modularize a big program in small packages — because it's better than to use dll/so.
Each one these packages will work in a bounded context. Each one may have a "Facade" to the main program, exposing its "interface" (classes and interfaces, most).
So, the main program shouldn't know nothing about "private units" that belongs only for the package.
That is encapsulation too.

Best regards,
Marcos Douglas

Reply by email

leledumbo
to Marcos Douglas B. Santos
Is it not possible *today* or never?

Never, not without changing the way compiler manages units. FPC units are organized in folders for easier management at filesystem level, but filesystem is not the namespace, there can never be /first/path/to/a.pas and /second/path/to/a.pas used in a single program, because only that a identifies the unit, not the path to it. This is the same as you can't have two files with the same name under the same path. Indeed you can't control unit names written by 3rd party and collision might happen, so most package developers add prefix in their unit names (e.g. IdGlobals) or use the new dotted unit names (e.g. my.globals.types, your.globals.consts).

So, the main program shouldn't know nothing about "private units" that belongs only for the package. That is encapsulation too.

Pascal unit is THE namespace, while most other languages uses filesystem as namespace. Namespace don't have visibility, only its contents have.

Reply by email

Sven Barth
to Marcos Douglas B. Santos: On Fri, Oct 13, 2017 at 6:40 PM, Sven Barth via Lazarus Is it not possible *today* or…

Am 14.10.2017 00:03 schrieb "Marcos Douglas B. Santos via Lazarus" <

Show quoted text 24 lines
: On Fri, Oct 13, 2017 at 6:40 PM, Sven Barth via Lazarus wrote: > On 13.10.2017 21:32, Marcos Douglas B. Santos via Lazarus wrote: >> [...] >> >> IMHO, would be better if packages may have just some "exported units". >> What do you think? > > It doesn't matter. Any symbols in a unit are mangled only locally to the > unit and there *can not* be two or more symbols with the same name in > one program. What you ask for is simply not possible. Is it not possible *today* or never? I don't know how the compiler works, but try to understand me: I would like to modularize a big program in small packages — because it's better than to use dll/so. Each one these packages will work in a bounded context. Each one may have a "Facade" to the main program, exposing its "interface" (classes and interfaces, most). So, the main program shouldn't know nothing about "private units" that belongs only for the package. That is encapsulation too.

I can very reasonably say that this will never be possible. It's simply how things work on the binary level. And as I said that's also true for dynamic packages (which are a different beast than Lazarus' packages): even if a unit becomes part of a package it won't be touched as it can be used as part of the package as well as directly in the application binary without any changes.
Unit names *must* be unique inside a program (with or without packages). So better use prefixes or dotted unit names to reduce the chance for conflicts.

Regards
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20171014/18034f30/attachment.html>

Reply by email

Marcos Douglas B. Santos
to leledumbo: Never, not without changing the way compiler manages units. FPC units are organized in…
leledumbo wrote 17 lines
> Is it not possible *today* or never? Never, not without changing the way compiler manages units. FPC units are organized in folders for easier management at filesystem level, but filesystem is not the namespace, there can never be /first/path/to/a.pas and /second/path/to/a.pas used in a single program, because only that a identifies the unit, not the path to it. This is the same as you can't have two files with the same name under the same path. Indeed you can't control unit names written by 3rd party and collision might happen, so most package developers add prefix in their unit names (e.g. IdGlobals) or use the new dotted unit names (e.g. my.globals.types, your.globals.consts). > So, the main program shouldn't know nothing about "private units" that > belongs only for the package. That is encapsulation too. Pascal unit is THE namespace, while most other languages uses filesystem as namespace. Namespace don't have visibility, only its contents have.

I know how it works in a single program, but I thought that using packages could be different... but not.
Thanks, anyway.

Best regards,
Marcos Douglas

Reply by email

Marcos Douglas B. Santos
to Sven Barth: Am 14.10.2017 00:03 schrieb "Marcos Douglas B. Santos via Lazarus" < I can very…
Sven Barth wrote 34 lines
Am 14.10.2017 00:03 schrieb "Marcos Douglas B. Santos via Lazarus" : > > On Fri, Oct 13, 2017 at 6:40 PM, Sven Barth via Lazarus > wrote: > > On 13.10.2017 21:32, Marcos Douglas B. Santos via Lazarus wrote: > >> [...] > >> > >> IMHO, would be better if packages may have just some "exported units". > >> What do you think? > > > > It doesn't matter. Any symbols in a unit are mangled only locally to the > > unit and there *can not* be two or more symbols with the same name in > > one program. What you ask for is simply not possible. > > Is it not possible *today* or never? > I don't know how the compiler works, but try to understand me: > I would like to modularize a big program in small packages — because > it's better than to use dll/so. > Each one these packages will work in a bounded context. Each one may > have a "Facade" to the main program, exposing its "interface" (classes > and interfaces, most). > So, the main program shouldn't know nothing about "private units" that > belongs only for the package. > That is encapsulation too. I can very reasonably say that this will never be possible. It's simply how things work on the binary level. And as I said that's also true for dynamic packages (which are a different beast than Lazarus' packages): even if a unit becomes part of a package it won't be touched as it can be used as part of the package as well as directly in the application binary without any changes. Unit names *must* be unique inside a program (with or without packages). So better use prefixes or dotted unit names to reduce the chance for conflicts.

My next question would be about dynamic packages... but you've already answered me.
Thanks for all information.

Best regards,
Marcos Douglas

Reply by email

leledumbo
to Marcos Douglas B. Santos: On Sat, Oct 14, 2017 at 1:18 AM, leledumbo via Lazarus I know how it works in a single…
I know how it works in a single program, but I thought that using packages could be different... but not.

Packages is Lazarus concept, the compiler has no idea about Lazarus does with it is generate a unit with the same name as the .lpk containing all units belonging to the package in its uses clause (with additional Register procedure and initialization section containing RegisterPackage call, purely for linking with the IDE), then feed the compiler with it. The end result is no different than compiling the units manually. In the end, when used by a program, it's the same combinations of .o and .ppu pair that the compiler sees.

Reply by email

Marcos Douglas B. Santos
to leledumbo
leledumbo wrote 11 lines
> I know how it works in a single program, but I thought that using > packages could be different... but not. Packages is Lazarus concept, the compiler has no idea about Lazarus does with it is generate a unit with the same name as the .lpk containing all units belonging to the package in its uses clause (with additional Register procedure and initialization section containing RegisterPackage call, purely for linking with the IDE), then feed the compiler with it. The end result is no different than compiling the units manually. In the end, when used by a program, it's the same combinations of .o and .ppu pair that the compiler sees.

I see. Thanks.

Regards,
Marcos Douglas

Reply by email