[fpc-pascal] Ideas for namespace implementation

2010-07-25 Thread Graeme Geldenhuys
Hi,

As I stated in another thread, namespaces are not perfect, but they do
work, and they do greatly reduce the chances for unitname or classname
conflicts. Other languages and frameworks (Java, .NET, C++ etc)
already showed this.

How 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):
  constants.pas
  utils.pas
  etc...

Extremely common names, and are really good names for what they
contain, so why can't we use them in our projects? Namespaces will
resolve this problem. What we need to discuss, is how to implement
namespace support in FPC.

Some ideas I can think of... please list your own if you have some:

1)  Follow the "dotted" notation for unit names as implemented in Delphi.
   Cons:
 - They force rather long unit names, which is frowned upon in the Pascal
   community (for whatever reason).

   Ambiguities couldn't 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:
  http://bugs.freepascal.org/view.php?id=14439


2) Another idea is in a way similar to how compiler defines work. This
idea tries
to resolve the problem in (1) with long unit names and with least amount of
resistance to existing code. A namespace can be applied
to a single unit, or to a group of units (think of it as a
"package"). To get this to
work, we will have to introduce a new keyword and a new compiler parameter.
Here follows examples of both.

// per unit
   unit utils namespace companyxyz;
   uses
 ...
   end.

... or applied at compile time to all units being compiled.

// our sample unit - as normal, nothing new
unit utils;
uses
  ...
end.

$ fpc -namespace=companyxyz utils.pas constants.pas ...

... if a conflict occurs, then you reference the unit with the
full namespace.
eg:

  uses
 companyxyz.utils;  // because unit names must be valid
identifier and thus,
  // can't contain dots, when the
compiler sees this, it knows
  // companyxyz. is a namespace reference.

Pros:
  - No long unit names are required.
  - We can use all the easy unit names for projects. eg:
utils.pas, constans.pas etc.
  - Can be applied to existing code without any code changes, simply by
adding a new namespace compiler parameter when compiling that existing
code. Thus, FPC's RTL and FCL, or fpGUI or Lazarus etc could
apply it too, to
their makefiles.
  - referencing via a namespace is only needed when a conflict occurs, other
than that, units are used as normal.
  - If no namespace is specified, then it automatically falls
under the "global"
namespace - maybe using the namespace value "global" and will be similar
to all units containing the following:
eg:
unit myunit namespace global;
...
end.

... or being compiled with 'fpc -namespace=global '


3)  Automatically add the directory where the unit is located, as the
namespace. Similar to what Java does.



I'm sure given more time, I could come up with some more ideas, but I
rather like (2) because it could be applied to all existing code,
without any code changes required.



-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: Ideas for namespace implementation

2010-07-25 Thread Graeme Geldenhuys
On 25 July 2010 19:16, Michael Van Canneyt wrote:
>
> What unit name conflicts ? In 15 years that I work with Delphi and FPC, and
> I have use many many packages, I encountered exactly 1 conflict:

Then you are extremely lucky. I have experience stacks of unit name
conflicts in my years of programming. Have you never had the urge to
use the unit name: constants.pas? utils.pas?



> the strutils of rxlib, which conflicted with strutils introduced in a later
> version of Delphi. The RX copy was renamed to rxstrutils, and that was it.

A perfect example of the problem!
  * Compilers get preference over the easy and well descibed unit
names, while other projects must now rename there units.
  * Other projects now have to live with crappy named units with ugly prefixes.
  * can't use underscores because for some reason it is frowned appon,
so now you
get odd looking units where the prefix ends with the same letter
as the original unit names.

Utilities functions that work on Strings - wouldn't it make sense to
have them in a unit: strutils.pas?
Currently we can't, even though the unit name perfectly describes the
function of that unit - so we have to shun from the ideal name. If we
don't, we are guaranteed to get conflicts in our projects. So now only
the "compiler" projects like Delphi or FPC my use such names - that is
crap.


> I am sorry, but I think that the seriousness of the problem is very much
> overestimated. Wanting to type rx.strutils versus rxstrutils. Really ?

And my suggestion (2) will reduce such syntax *only* when a conflict
has occured, without long unit names.


> And let us not forget: most component vendors must support older versions of
> Delphi, so they are barred from using the dotted unit name syntax anyway.

Which option (2) I suggested will also handle via a namespace compiler
parameter or the 'namespace ' in the unit, but declared inside
a IFDEF checking for compiler version: eg

unit utils {$IFDEF Ver2_4_x} namespace myproject {$ENDIF};
uses
  ...
end.


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Re: Ideas for namespace implementation

2010-07-25 Thread Michael Van Canneyt



On Sun, 25 Jul 2010, Graeme Geldenhuys wrote:


On 25 July 2010 19:16, Michael Van Canneyt wrote:


What unit name conflicts ? In 15 years that I work with Delphi and FPC, and
I have use many many packages, I encountered exactly 1 conflict:


Then you are extremely lucky. I have experience stacks of unit name
conflicts in my years of programming. Have you never had the urge to
use the unit name: constants.pas? utils.pas?



No. Because I don't see the difference between having to type

uses
  wisa.utils;

versus

uses wisautils;

The latter is even shorter. So I really don't see the gain.

The discussion is not about namespaces. Object Pascal HAS namespaces,
namely units. The discussion is about 'enhancing' the namespace to allow 
one or more dots in the name.


That said, I don't really care whether the . ends up in the compiler or not,
but I do think it is much ado about nothing. Yes, it may be nice to have,
but no, it is not urgent and not the end of the world if we don't do it.

Unicode strings is orders of magnitude more important.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Re: Ideas for namespace implementation

2010-07-25 Thread Graeme Geldenhuys
On 25 July 2010 23:30, Michael Van Canneyt wrote:
>
> The discussion is not about namespaces. Object Pascal HAS namespaces,
> namely units. The discussion is about 'enhancing' the namespace to allow one
> or more dots in the name.

With the importance that the '.' is only needed when resolving a
conflict - all other instances, you will just use the unit name as
normal. Also, the '.' usage is only limited to the uses clause. At
least we will be able to easily use more logical unit names without
the fear of getting unit name conflicts.


> Unicode strings is orders of magnitude more important.

I wasn't arguing what is/isn't more important that other tasks. I'm
gathering information and feedback on a feature request to enhance
namespace support to reduce unit name conflicts. Just because Unicode
strings haven't been implemented yet, doesn't mean we maybe implement
something else.

Regarding the cpstrnew branch:
I'll try and resolve my Unicode String branch (cpstrnew) compilation
issues this coming week (or at least report the issue with more
details). Since the resync with Trunc, I can't compile that branch,
and thus can't submit my local changes I have so far.


-- 
Regards,
  - Graeme -


___
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal