[Gambas-user] Static Classes and Modules

Benoît Minisini gambas at ...1...
Sat Sep 20 20:24:46 CEST 2014


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




More information about the User mailing list