Property Definition does not allow STATIC

General FreeBASIC programming questions.
Post Reply
wallyg
Posts: 270
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Property Definition does not allow STATIC

Post by wallyg »

I have an object (Type) that has some global values and I wanted to be able to use the Property syntax for setting this global value as

Code: Select all

Type 					Gobject
Static as Integer			_Prop1
...
Declare Static Property		Prop1(value as Integer)
...
End Type

and later set the value as         

gObject.Prop1 = 17

I was surprised to find that STATIC was not allowed in the documentation.

Is it a possibility that this might be allowed in the future?

Wally
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property Definition does not allow STATIC

Post by fxm »

A "feature request" is already filled in since 2013:
#279 Add the static properties feature
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property Definition does not allow STATIC

Post by fxm »

But this new feature seems to be not very easy, mainly because of a parser problem.

Example of the parser syntax problem for STATIC PROPERTIES without index parameter:
There is a syntax ambiguity between one of property result assignment syntaxes inside the get-property body and the set-property calling:

Code: Select all

Type typename
    Declare Static Property staticPropertyName () As Integer
    Declare Static Property staticPropertyName (Byval i As Integer)
    Static As Integer staticVariableName
    Dim As Integer dummy
End Type
Dim As Integer typename.staticVariableName = 0

Static Property typename.staticPropertyName () As Integer
    Return staticVariableName
'or Property = staticVariableName
'or typename.staticPropertyName = staticVariableName  ''ambiguity with below line
End Property

Static Property typename.staticPropertyName (Byval i As Integer)
    staticVariableName = i
End Property

Dim As Integer variableName = 1
typename.staticPropertyName = variableName            ''ambiguity with above line
Print typename.staticPropertyName
the same expression 'typename.staticPropertyName =' is used for 2 different things.

The similar ambiguity for a static function returning by reference is solved by using 'typename.staticFunctionName =' inside the function body, and a mandatory 'typename.staticFunctionName() =' at outside.
But parentheses cannot be used for a set-property calling.

Should the parser distinguish for the same expression depending on whether it is used inside or outside the property body, or should this expression inside the property body be prohibited ( there would be only 2 left available) ?


Conclusion:
Already, when a non-static get-Property is defined with one index parameter, the 'typename.PropertyName =' syntax can not be used inside the property body to return a value. For such a get-Property, the 'Property =' syntax (in addition to the 'Return' syntax) is only the one allowed.
Therefore, for a static get-Property, the ''typename.PropertyName =' syntax could be also disallowed inside the property body.
In conclusion, the 'typename.PropertyName =' syntax could be disallowed inside the property body when the property is defined with one index parameter or the property is static.

I will update the feature request with this parser problem.
Last edited by fxm on Apr 20, 2024 8:59, edited 1 time in total.
Reason: Added conclusion.
paul doe
Moderator
Posts: 1738
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Property Definition does not allow STATIC

Post by paul doe »

@wallyg: Do you need to perform some validation of the data in the property? Because if not, then just a static variable suffices...
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property Definition does not allow STATIC

Post by fxm »

Alternatively, you can use a non-static member property (which accesses the static integer member) and call it on any instance opportunity, or on a null pointer or null reference to the 'Type' (provided the property body does not use 'This').

Code: Select all

Type Gobject
    Public:
        Declare Property Prop1(Byval I As Integer)
        Declare Property Prop1() As Integer
    Private:
        Static As Integer _Prop1  '' authorized value between -20 and +20
        Dim As Integer __
End Type
Dim As Integer Gobject._Prop1 = 0

Property Gobject.Prop1(Byval I As Integer)
    If Abs(I) <= 20 Then Gobject._Prop1 = I
End Property

Property Gobject.Prop1() As Integer
    Property = Gobject._Prop1
End Property

' Example using a null reference to Gobject:
Dim Byref As Gobject nullGobject = *Cptr(Gobject Ptr, 0)

nullGobject.Prop1 = 25
Print nullGobject.Prop1

nullGobject.Prop1 = 17
Print nullGobject.Prop1

Sleep
wallyg
Posts: 270
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Property Definition does not allow STATIC

Post by wallyg »

Yes, I wanted to perform some validation and possibly update other global values based on the specified setting.

In addition, all the specific instance routines that are available to the user of my library of objects use properties and they understand the syntax of that call for setting values. Using a Static Sub as a replacement is a bit annoying in that it would be a different syntax.

But it is what it is and I will just have to live with it.

I assume that this will not be forthcoming in the foreseeable future and just work around it.

Thanks for all the advice and I will look into the alternative suggested in the previous response.

Grateful as always for all the helpful advice.

Wally
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property Definition does not allow STATIC

Post by fxm »

fxm wrote: Apr 12, 2024 11:51 Alternatively, you can use a non-static member property (which accesses the static integer member) and call it on any instance opportunity, or on a null pointer or null reference to the 'Type' (provided the property body does not use 'This').

Code: Select all

Type Gobject
    Public:
        Declare Property Prop1(Byval I As Integer)
        Declare Property Prop1() As Integer
    Private:
        Static As Integer _Prop1  '' authorized value between -20 and +20
        Dim As Integer __
End Type
Dim As Integer Gobject._Prop1 = 0

Property Gobject.Prop1(Byval I As Integer)
    If Abs(I) <= 20 Then Gobject._Prop1 = I
End Property

Property Gobject.Prop1() As Integer
    Property = Gobject._Prop1
End Property

' Example using a null reference to Gobject:
Dim Byref As Gobject nullGobject = *Cptr(Gobject Ptr, 0)

nullGobject.Prop1 = 25
Print nullGobject.Prop1

nullGobject.Prop1 = 17
Print nullGobject.Prop1

Sleep

If 'Gobject' has an explicit or even implicit default constructor (member data initializer, member string, inheriting of a type with constructor or extending 'Object', ...), the property can be call on a simple temporary instance like mainly 'Gobject()' (or '(Gobject)'):
  • - For example, if 'Gobject' has a member data initializer:

    Code: Select all

    Type Gobject
        Public:
            Declare Property Prop1(Byval I As Integer)
            Declare Property Prop1() As Integer
        Private:
            Static As Integer _Prop1  '' authorized value between -20 and +20
            Dim As Integer __ = 0
    End Type
    Dim As Integer Gobject._Prop1 = 0
    
    Property Gobject.Prop1(Byval I As Integer)
        If Abs(I) <= 20 Then Gobject._Prop1 = I
    End Property
    
    Property Gobject.Prop1() As Integer
        Property = Gobject._Prop1
    End Property
    
    ' Example using a temporary instance of Gobject (which has a member data initializer):
    
    Gobject().Prop1 = 25
    Print Gobject().Prop1
    
    Gobject().Prop1 = 17
    Print Gobject().Prop1
    
    Sleep
    
    - Another syntax available consists of using a 'With Gobject...End With' block, which allows to first construct the temporary 'Gobject' instance before composing it with each '.' operator (the temporary instance is maintained up to the 'End With'):

    Code: Select all

    Type Gobject
        Public:
            Declare Property Prop1(Byval I As Integer)
            Declare Property Prop1() As Integer
        Private:
            Static As Integer _Prop1  '' authorized value between -20 and +20
            Dim As Integer __ = 0
    End Type
    Dim As Integer Gobject._Prop1 = 0
    
    Property Gobject.Prop1(Byval I As Integer)
        If Abs(I) <= 20 Then Gobject._Prop1 = I
    End Property
    
    Property Gobject.Prop1() As Integer
        Property = Gobject._Prop1
    End Property
    
    ' Example using a temporary instance of Gobject (which has a member data initializer):
    
    With Gobject
        .Prop1 = 25
        Print .Prop1
    
        .Prop1 = 17
        Print .Prop1
    End With
    
    Sleep
    
Post Reply