Sign in Join
Wiki copytemporary

Namespaces: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Since [[FPC New Features 3.0|FPC 3.0.0]] unit namespaces with dots are supported.
22
33## Namespaces in FPC
44
55This page contains proposals and ideas, which could lead to the a better implementation of namespaces that what Delphi currently has. These ideas may or may not work, that's the whole point of this wiki page. Throw ideas around until we find something that could work.
66
77## Why?
88
99Namespaces are not perfect, but they do work, and they do greatly reduce the chances for Unit name or Class name conflicts. Other languages and frameworks (Java, .NET, C++ etc) have already seen the problem and introduced namespace support.
1010
1111How visible is the unit name conflict problem? Just think, how many projects are there that use the unit names (or would like to use these common names):
1212
1313` constants.pas`\
1414` utils.pas`\
1515` etc...`
1616
1717Extremely common names, and are really good names for what they contain (they describe the purpose of the unit clearly), so why can't we use them in our projects? Namespaces will resolve such problem.
1818
1919- To reduce the chances of conflicting unit names
2020- Allow developers to use ideal names for units without the worry of conflicting unit names with the FPC compiler or other component libraries. eg: constants.pas or utils.pas or strutils.pas
2121- No need for cryptic 2 or 3 letter prefixes to unit names.
2222
2323## Interested Parties
2424
2525- Graeme Geldenhuys
2626- Marcos Douglas
2727
2828## Usage Examples
2929
3030Please add any new ideas to the end of the list.
3131
3232### Suggestion 1 - "dotted unit name notation"
3333
3434Use the same style as implemented in Delphi 2009 onwards.
3535
3636#### Pros
3737
3838- Extends the syntax in Pascal where units are already namespaces (disambiguating two names by prefixing the unit name)
3939- Similar to the Ada approach of packages, much more robust than namespaces
4040- Delphi compatible
4141
4242#### Cons
4343
4444- The force rather long unit names. eg: freepascal.rtl.classes.pas
4545- Unit names now contain a '.' character which doesn't make them truly valid identifiers.
4646- Ambiguities could appear, but they are rather easy to overcome with a simple language rule. Object Pascal already have many language rules, this will just be a new one. See the following Mantis report for details: [Mantis report 14439](http://bugs.freepascal.org/view.php?id=14439)
4747
4848### Suggestion 2 - new keyword & new compiler parameter
4949
5050This idea works similar to Compiler Defines. They can be applied to a unit, or to a project as a whole. Add a new keyword **namespace \<value\>** to the Object Pascal language that gets added to the *unit* line of a source file. Also add a new **-namespace=\<value\>** compiler parameter to apply a namespace to a whole project
5151
5252#### Example
5353
5454`   // per unit`\
5555`  unit utils namespace companyxyz;`\
5656`  uses`\
5757`    ...`\
5858`  end.`
5959
6060...or applied at compile time to all units being compiled.
6161
6262`   // our sample unit - as normal, nothing new`\
6363`   unit utils;`\
6464`   uses`\
6565`     ...`\
6666`   end.`
6767
6868`   $ fpc -namespace=companyxyz utils.pas constants.pas ...`
6969
7070... if a conflict occurs, then you reference the unit with the full namespace.
7171
7272`     uses`\
7373`        companyxyz.utils;  // because unit names must be valid identifier and thus,`\
7474`                           // can't contain dots, when the compiler sees this, it knows`\
7575`                           // companyxyz. is a namespace reference.`
7676
7777#### Pros
7878
7979- Namespaces can be applied per unit or for a whole project. The unit specific namespace takes preference compared to the namespace declared as a compiler parameter.
8080
8181` unit buttons namespace fpgui;`\
8282` uses`\
8383`   Classes`\
8484`  ...`\
8585` end.`
8686
8787- Namespaces can be applied via the compiler command line parameter so no need to modify each individual unit.
8888
8989` fpc -namespace=fpgui buttons.pas groupbox.pas panel.pas`
9090
9191- Least amount of intrusion to existing code.
9292- Unit names still stay short
9393- Unit names are still valid identifiers (unlike the "dotted" notation of Delphi).
9494- If no explicit namespace is declared in the unit, or per compiler parameter, then in automatically get added to the "global" namespace, and works like FPC and Delphi prior to v2009.
9595- The usage of the namespace is only required when you get a unit name conflict. All other times, simply using the unit name as-is will suffice.
9696- When a unit references a unit in a uses clause without the namespace, then the following search priority is taken.
9797 - "global" (or undefined) namespaces take preference. For example the RTL in FPC will not be compiled with a namespace to get higher priority. So if a project also contains a SysUtils.pas unit, the RTL's SysUtils will take preference, and the developer will have to introduce a project wide namespace.
9898 - Then units part of a project.
9999 - Then units defined in a namespace.
100100
101101`// our project also has a SysUtils unit, but because RTL already has one, we`\
102102`// must define a namespace for it.`\
103103`unit SysUtils namespace myproject;`\
104104`uses`\
105105`  ...`\
106106`end.`\
107107` `\
108108`unit mytools;`\
109109`uses`\
110110`  SysUtils,             // from the RTL`\
111111`  myproject.SysUtils,   // from this project / the myproject namespace.`\
112112`  Classes;`
113113
114114#### Cons
115115
116116- In corner cases, some ambiguities could appear (if the developer doesn't understand the preferences of namespaces).
117117- The name of a common RTL unit (e.g: sysutils) can be redefined. And while being fully qualified in the uses-clause "myproject.SysUtils", it could occur unqualified in the source (if only the one SysUtils was used): "SysUtils.Foo" or "SysUtils.Exception" (Exception could be redefined in myproject)
118118
119119
120120This would be confusing to any reader (other than the original author). As reading "SysUtils" will always trigger the assumption RTL
121121
122122- how will the compiler know what file contains myprojects.sysutils ? It will have to index all files in the search path on every compiler start. The Java solution solves at least that, check all classpath roots for a fixed directory. ( for x in classpaths do check(x+'/myproject/sysutils').
123123
124124### Suggestion 3 - directory layout
125125
126126Similar to what JAVA does, by using the directory hierarchy to define the namespace.
127127
128128#### Pros
129129
130130- Keeps the unit names short, while still mapping the full namespace qualified name directly to a path name
131131
132132- Only need a single search path to include all packages, since each package is automatically mapped to its own subdirectory
133133
134134#### Cons
135135
136136- I see lots of issues with this idea because FPC supports multiple search and library paths per project. So this is probably the crappiest idea. :-)
137137
138138### Mapping Namespace to each search path
139139
140140Each installed "package" (set of units) usually resides in a directory, and has it's own /lib or units folder for pre-compiled ppu.
141141
142142This directory is added to the list of search paths.
143143
144144#### Definitions / Proposals
145145
146146- Terms used:
147147
148148:* Package: A collection of units, grouped in one directory. (Since FPC as no real packages yet)
149149
150150
151151
152152Examples: RTL, FCL, LCL, SynEdit, CodeTools, RxLib, ....
153153
154154- Notes:
155155
156156:* Unit names:
157157
158158
159159
160160It is not encouraged to call a unit Utils. It should still be attempted to use names with a likelihood for uniqueness (such as RxUtils). The described proposal is a last resort to resolve remaining conflicts
161161
162162#### Adding / Creating a Namespace / SearchPath-Alias
163163
164164
165165Current Situation; Any package used in a project must be known to FPC at compile time of the project. that is archived by adding an -Fu to either the command-line or fpc.cfg file
166166
167167- Proposal 1: Extending -Fu
168168
169169
170170Allowing to specify Namespace/SearchPath-Alias together with the Path in the -Fu (or alternative a new directive -FU)
171171
172172Since there is no char, that can not already be part of a directory name in a path, -Fu cannot easily be extended, so that an alias would always be detected.
173173
174174Using -FU would tell the compiler, that an alias is always expected. The alias always ends at the first occurence of the separator char. e.g -FU LCL : /path/to/lcl (spaces are for readability =\> real directive, would not have spaces)
175175
176176- Proposal 2: Adding a default namespace to the Lazarus *.lpk file (Lazarus would add it to the -FU)
177177
178178
179179This would be set by the supplier of the package, e.g when downloading rxlib, the people who are in charg of rxlib, would have set the default to "rxlib"
180180
181181This can create conflicts in namespace,. but the user can easily change the default
182182
183183
184184If the user changes the default, and hiscode refers to the changed default, he must change the default on all installations where he uses this.
185185
186186Maybe the ability override defaults, in the project settings instead should be discussed? Separate discussin...
187187
188188- Proposal 3: Adding a default namespace inside one/each unit of the package (using a namespace directive, after the unit declaration)
189189
190190
191191
192192Instead of a namespace in -FU or lpk; a namespace can be defined inside the unit(s)
193193
194194Because there is no package file, this would be needed in every unit. (alternatively a unit with special name (e.g equal to the last directory name in the path ?)
195195
196196Example
197197
198198
199199Unit Utils; Namespace 'RxLib'; \[1\]
200200
201-<b>Cons: As there currently is no package concept, this would be required for each unit, here would be no way to ensure all units in the path have the same (a consistent) Namespace.</b>
201+Cons: As there currently is no package concept, this would be required for each unit, here would be no way to ensure all units in the path have the same (a consistent) Namespace.
202202
203-<b>Multiple namespaces in one packages may not be desirable .</b>
203+Multiple namespaces in one packages may not be desirable .
204204
205205- Mixing of Proposals:
206206
207207
208208
209209-Fu (1, 2) and namespace (3) can work in parallel, -FU takes priority.
210210
211211- Multiply Namespace: Needed ?
212212
213213- Project overrides for conflicting Namespaces: Todo
214214
215215#### The "uses" clause
216216
217217- Proposal 1: using "in"
218218
219219
220220
221221uses Utils in RxLib;
222222
223223
224224Note ther e are no quotes around the namespace, this disambiguates it from a directory of the same name, present in the project folder.
225225
226226uses Utils in RxLib alias RxUtils, utils in LCL alias LCLUtils;
227227
228228uses Utils in RxLib as RxUtils, utils in LCL as LCLUtils;
229229
230230
231231alias (or as / to be decided) can be used, if several units of the same name exist across several namespaces, and should all be imported.
232232
233233- If no alias is given the unit is known by it's own name
234234- If an alias is given the unit is known by the alias, and the alias only
235235
236236No 2 units must resolve to the same name
237237
238238- It may be proposed, that if 2 units Utils exists, both must be aliased. Even though, if only one was aliased, they would have different names already.
239239
240240
241241But that might lead to unnecessary changes in a unit if a unit name is already used to distinguish types like in the following example:
242242
243243`uses`\
244244`  Windows, Graphics; // both have a type TBitmap`\
245-\
245+
246246`(...)`\
247-\
247+
248248`var`\
249249` b1: TBITMAP; // from Windows`\
250250` b2: Graphics.TBitmap; // Note: for this example I assume that the first unit "wins". If FPC handles this different, then please exchange the unit in the uses clause ^^`
251251
252252
253253
254254Assuming there are more usages of "Graphics.TBitmap" in that unit the addition of another Graphics unit would lead to unnecessary changes if both units must be aliased:
255255
256256`uses`\
257257` Windows, Graphics in LCL, Graphics in SomeGraphicsPkg as SomeGraphics;`\
258258` // Note: I just picked one of the syntaxes for this example`
259259
260260- Proposal 2: Using "." (dot)
261261
262262
263263
264264uses RxLib.Utils;
265265
266266uses RxLib.Utils as RxUtils;
267267
268268- Proposal 3: Using alternatively "as"/"alias" syntax
269269
270270
271271
272272uses RxUtils=RxLib.Utils;
273273
274274uses RxUtils(RxLib.Utils);
275275
276276#### References in the code
277277
278278In the code the unit can only be referenced by it's alias (or it's own name, if it was not aliased.
279279
280-<b>Namespace qualifiers should not be allowed in the code</b>
280+Namespace qualifiers should not be allowed in the code
281281
282282Namespace qualifiers in the code can cause ambiguity, since a unit and a name of the same namespace can exist. See [Mantis report 14439](http://bugs.freepascal.org/view.php?id=14439)
283283
284284Namespace qualifiers are not native pascal. Keeping their effect as low as possible (quarantined to the "uses" clause, maybe the "unit" declaration) would be desirable
285285
286286#### Pros
287287
288288#### Cons
289289
290290### Suggestion 123 - all together
291291
292292#### Proposal
293293
294294Use dotted unit names with additional keywords and mapped to directory layout.
295295
296296#### Example
297297
298298E.g. the current classes unit would become freepascal.rtl.classes.pas
299299
300300But you can also store it in a directory freepascal/rtl/classes.pas or freepascal.rtl/classes.pas to avoid cluttering a directory with a lot of files and long names.
301301
302302When using it, you can use a new keyword to not write the full name for every unit.
303303
304304E.g.
305305
306306`  uses with freepascal.rtl do begin classes, sysutils end, with lazarus.lcl do begin ...  end, ...;`
307307
308308or allow multiple uses
309309
310310`  with freepascal.rtl do uses classes, syutils;`\
311311`  with lazarus.lcl do uses ...`\
312312`  ...`
313313
314314#### Pros
315315
316316- Combines the pros of each suggestion(see above)
317317
318318#### Cons
319319
320320- uses with needs to be repeated in every unit + some extra keywords (WITH, DO BEGIN.. END). Savings not that great, unless you really need a lot of units from one namespace. But since FPC/Delphi allows multiple classes per unit, this will be not the case as often as in e.g. C# or Java.
321321
322322## see also
323323
324324- [§ “Namespaces: Dotted Units” in the *Free Pascal reference guide*](https://www.freepascal.org/docs-html/ref/refse112.html)
325325
326326---
327327*From the Free Pascal wiki, page "Namespaces", as it stood on 2020-08-03 after a change by BrainChemistry, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
328328Category: Proposals

Back to history