Sign in Join

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

Mailing lists / fpc-pascal / 2020-10

Merging units

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Ryan Joseph

Something that's bothered me for a while I wanted to ask about.

I have a package I want to distribute which has 2 or more units, all of which are required to use the package. Technically all the code should be in one file but because units are a nice feature of Pascal we like to keep our code separate when we develop.

Usually what happens is we compromise and do either:

1) Have the user of the package add all the units to the uses clause of every file which needs it. This is annoying for the user because it bloats the uses section but for the developer our code is nicely separated into units.

2) Break the units into include files and wrap the interface/implementation blocks in ifdefs. This is nice for the user because they only have a single unit to use but now the developers of the package have to work in what are essentially header files (like in C). The RTL is of course filled with examples like this.

It feels to me like Pascal is missing a way to have the compiler help you make these "umbrella" units so we default back to C-like techniques which we've been using since the 90's.

I see things like this from other newer languages and it seems like something Pascal should have given the already existing module structure we take for granted but languages like C don't have.

unit test;
imports
  (A, B, C) from SomeUnit, OtherUnit; // types/functions are imported into the unit and accessible from the outside
end.

Any ideas of solutions or preferred approaches? Is it feasible to add an "imports" clause to Pascal or are there other pitfalls?

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote
Any ideas of solutions or preferred approaches? Is it feasible to add an "imports" clause to Pascal or are there other pitfalls?

The preferred solutions are indeed either to put everything in one unit (includes or not) or to have the user add multiple units. Though the later isn't that much of a problem when one uses a capable IDE that can help here.

Regards,
Sven

Reply by email

Marco van de Voort
to Ryan Joseph: Something that's bothered me for a while I wanted to ask about. I have a package I want…

Op 2020-10-02 om 19:55 schreef Ryan Joseph via fpc-pascal:

Show quoted text 9 lines
Something that's bothered me for a while I wanted to ask about. I have a package I want to distribute which has 2 or more units, all of which are required to use the package. Technically all the code should be in one file but because units are a nice feature of Pascal we like to keep our code separate when we develop. Usually what happens is we compromise and do either: 1) Have the user of the package add all the units to the uses clause of every file which needs it. This is annoying for the user because it bloats the uses section but for the developer our code is nicely separated into units. 2) Break the units into include files and wrap the interface/implementation blocks in ifdefs. This is nice for the user because they only have a single unit to use but now the developers of the package have to work in what are essentially header files (like in C). The RTL is of course filled with examples like this.

In some cases you can also move types and consts to a secondary unit, but alias them in the primary one.

Like e.g. Unixtype (what unit you say? Never heard of it? That is because the types are aliased into core units baseunix and unix)

Reply by email

Zamrony P. Juhara
to Marco van de Voort

I use a secondary unit as alias of all separate units as Marco suggests in https://github.com/fanoframework/fano/blob/master/src/fano.pas However I am curious if merging units like this has drawback. AFAIK smart linking works.

Sent from Yahoo Mail on Android

On Sat, Oct 3, 2020 at 3:31, Marco van de Voort via fpc-pascal wrote: Op 2020-10-02 om 19:55 schreef Ryan Joseph via fpc-pascal:

Show quoted text 9 lines
Something that's bothered me for a while I wanted to ask about. I have a package I want to distribute which has 2 or more units, all of which are required to use the package. Technically all the code should be in one file but because units are a nice feature of Pascal we like to keep our code separate when we develop. Usually what happens is we compromise and do either: 1) Have the user of the package add all the units to the uses clause of every file which needs it. This is annoying for the user because it bloats the uses section but for the developer our code is nicely separated into units. 2) Break the units into include files and wrap the interface/implementation blocks in ifdefs. This is nice for the user because they only have a single unit to use but now the developers of the package have to work in what are essentially header files (like in C). The RTL is of course filled with examples like this.

In some cases you can also move types and consts to a secondary unit, but alias them in the primary one.

Like e.g. Unixtype (what unit you say? Never heard of it? That is because the types are aliased into core units baseunix and unix)

Reply by email

Ryan Joseph
to Zamrony P. Juhara
Show quoted text 7 lines
On Oct 6, 2020, at 2:52 PM, Zamrony P. Juhara via fpc-pascal wrote: I use a secondary unit as alias of all separate units as Marco suggests in https://github.com/fanoframework/fano/blob/master/src/fano.pas However I am curious if merging units like this has drawback. AFAIK smart linking works.

Got around to trying this and the namespace aliases are nice. Thanks.

What about functions though? This will require lots of inline wrappers. Having a function alias would be a really nice feature but I think Sven actually rejected this idea some while ago when I asked.

Regards,
Ryan Joseph

Reply by email