[Gambas-user] Static Classes and Modules

John Leake jleake at ...3375...
Sat Sep 20 23:13:31 CEST 2014


Hi All,
It is clear that many people have a huge investment in the status-qua.
Just update the docs and say what can and cannot be compared (like the
address of the object in this case).  Then at least users are well
informed before they code and rely on what they discover through
experimentation.

On 20/09/14 22:01, B Bruen wrote:
> On Sat, 20 Sep 2014 20:24:46 +0200
> Benoît Minisini <gambas at ...1...> wrote:
> 
>> Le 20/09/2014 11:55, John Leake a écrit :
>>> Thanks Tobi,
>>>>> So could someone tell me if a class without any dynamic variable ie a
>>>>> static class, can or cannot be created ?
>>>>>
>>>>
>>>> Or you can try it out.
>>>>
>>>> What I did was creating a module and making objects from it with New -- it
>>>> works, but those objects are even more limited than I had expected. In their
>>>> code, there seems to be no way to even see that they are any different from
>>>> the automatic instance (singleton). The module's code was:
>>>>
>>>> --8<--[ Module1.module ]----------------------------------------------------
>>>> ' Gambas module file
>>>>
>>>> Public Sub Print()
>>>>    Print Me
>>>> End
>>>> --8<------------------------------------------------------------------------
>>>>
>>>> and the program:
>>>>
>>>> --8<--[ MMain.module ]------------------------------------------------------
>>>> ' Gambas module file
>>>>
>>>> Public Sub Main()
>>>>    Dim h As New Module1, g As New Module1
>>>>
>>>>    Print Module1
>>>>    Module1.Print()
>>>>    Print "---"
>>>>    Print h
>>>>    h.Print()
>>>>    Print "---"
>>>>    Print g
>>>>    g.Print()
>>>> End
>>>> --8<------------------------------------------------------------------------
>>>>
>>>> with the output:
>>>>
>>>>    (Class Module1)
>>>>    (Class Module1)
>>>>    ---
>>>>    (Module 0x...b8)
>>>>    (Class Module1)
>>>>    ---
>>>>    (Module 0x...e8)
>>>>    (Class Module1)
>>>>
>>>> So indeed, there are different objects (especially, it's possible to
>>>> instantiate modules) but "Me" inside the module code always refers to
>>>> the singleton.
>>>>
>>> This is where I have a problem h and g are different.
>>>
>>
>> Static classes should not be instanciable. So don't do that even if the 
>> interpreter allows it. It's there from the beginning, and it's probably 
>> a mistake.
>>
>> I must investigate before doing anything, because it may be a 
>> backward-compatibility problem: I sometimes have to keep bugs for that 
>> reason, and I must wait for a major release to fix it.
>>
>> Regards,
>>
>> -- 
>> Benoît Minisini
>>
> 
> One small argument in favour of allowing instantiation of modules and "static" classes.
> 
> We have a component called "GenUtil" that contains some utility functions and data that we use in lots and lots of our projects. Originally (some time back in Gambas2) it contained a class "StrUtil" that was coded as a class and thus needed instantiation in all the client projects that used it. Some time ago while modifying the library code I decided that, since the StrUtil methods were essentially static in nature and as the class had no dynamic data, I could change it to a module.  Newer projects now access those methods via the module name as they should.  But the older projects, that instantiate the module as an object and refer to the methods via the object name still work.  I just checked and we have over 80 projects that reference that component.  I have no idea how many of them use which means of referencing the StrUtil methods.  So I would prefer that status quo remains.
> 
> In fact I would prefer that only those classes coded as CREATE PRIVATE would exclude instantiation as even though a class bereft of dynamic data or methods today may have some dynamics tomorrow and vice versa.  In short the decision that a class should prevent instantiation should be explicit i.e. via the CREATE PRIVATE command.
> 
> 
> regards
> Bruce
> 




More information about the User mailing list