[Gambas-user] Proposal for namespace support
Benoît Minisini
g4mba5 at gmail.com
Wed May 12 02:14:30 CEST 2021
Le 12/05/2021 à 01:06, Christof Thalhofer a écrit :
> Hi Benoît,
>
> Am 11.05.21 um 23:30 schrieb Benoît Minisini:
>
>> Here is the result of my thoughts about namespace support.
>
> Thank you!
>
>> I tried to find the simpler and less disturbing solution for the
>> interpreter code.
>>
>> 1) A new syntax for exporting classes.
>>
>> ' Class MyClass
>> Export As NameSpace:PublicName
>>
>> will export the class MyClass in the global table as "NameSpace:PublicName"
>
> Looks good for me if we see the CamelCase in the IDE. Here I think of
> things like
>
> ComCompanyDb:Orm
> ComCompanyCrypt:Hash
>
>> The MyClass name is still available inside the code of the current project.
>
> Is the class also available inside the project under its Namespace
> entry? This would make it easier to test it with gb.test.
Only if it is exported.
Exporting makes the string "NameSpace:ClassName" points at the class
MyClass inside the global symbol table.
>
>> Note that the old 'Export' keyword alone would be an equivalent of
>> 'Export As MyClass'
>
> If we could do as a shortcut:
>
> ' Class MyClass
> Export As NameSpace:
>
> and this were extended to 'NameSpace:MyClass', that would be fine. Here
> I think about typos where one would name the class file 'MyClass', then
> writes 'Export As NameSpace:MzCass'.
I don't really like the syntax, but I get the point.
>
> Another Point: Can we make an entry in the '.project' file
> 'namespace=MyNameSpace' and if it is filled every class in the project
> is automatically exported with 'MyNameSpace'? Except classes that the
> author explicitly exported with another namespace.
>
> This would also prevent us from typos. Is that complicated?
>
Not a good idea to export all classes of a project.
But we can imagine that 'Export' alone will export to the default
namespace of the project, and that the "default default" namespace is
the global symbol table.
>> 2) A new syntax for class names.
>>
>> Dim A As NameSpace:PublicName
>>
>> will automatically get the class named 'NameSpace:PublicName class' in
>> the global table by the export made in 1)
>
> With static classes this happens analogously? So that we can write:
>
> 'NameSpace:MyModule.MyMethod()'
I didn't think about class names used for calling static methods.
Yes, logically we should be able to do that. It's just problematic for
the compiler parser... must think about that.
>
> If this becomes too long, other languages provide aliases. For example PHP:
>
> https://www.php.net/manual/en/language.namespaces.importing.php
>
> has the syntax
>
> 'use MyReallyVeryLongNamespace as MyLong;'
This is a common feature I don't really want. Because then you can
mismatch the reader of the code, which cannot be sure which namespace we
refer at first sight.
Regards,
--
Benoît Minisini
More information about the User
mailing list