Finer control of access within a Type ... Extends
Finer control of access within a Type ... Extends
I am developing a library of Objects to be used by some clients.
When I define a section of variables in a Type definition, I place Private, Protected, or Public prior to each set of definitions.
However, I would like to be able to have a section of definitions that are Protected or Private for Write Operations, but Public for Read operations. Without that capability, I have to define a lot of Property definitions of the following form.
Property xxx
Return This.yyy Where yyy is a Protected variable of TypeObject
End Property
I am sure the reference TypeObject.yyy (where yyy is Public-Readonly) is significantly faster than TypeObject.xxx and the resulting dll is also significantly larger for the Property version.
Any chance to consider this capability for some future version?
When I define a section of variables in a Type definition, I place Private, Protected, or Public prior to each set of definitions.
However, I would like to be able to have a section of definitions that are Protected or Private for Write Operations, but Public for Read operations. Without that capability, I have to define a lot of Property definitions of the following form.
Property xxx
Return This.yyy Where yyy is a Protected variable of TypeObject
End Property
I am sure the reference TypeObject.yyy (where yyy is Public-Readonly) is significantly faster than TypeObject.xxx and the resulting dll is also significantly larger for the Property version.
Any chance to consider this capability for some future version?
Re: Finer control of access within a Type ... Extends
For fun, seeing the forum is quiet.
Code: Select all
type tobject extends object
declare constructor()
declare destructor
protected:
as double yyy
declare sub finish()
as double zzz
as zstring * 30 s
end type
constructor tobject()
yyy=89
zzz=34
s="Hello"
end constructor
destructor tobject
finish
end destructor
sub tobject.finish()
print "Done"
end sub
dim as tobject t
var p= cast(double ptr,cast(object ptr,@t))
print *(p+1)
print *(p+2)
print *cast(zstring ptr,(p+3))
*cast(zstring ptr,(p+3))="Bye, Press any key . . ."
print *cast(zstring ptr,(p+3))
t.destructor
sleep
Re: Finer control of access within a Type ... Extends
Less funny with the 32-bit compiler.dodicat wrote:For fun.
...
edit: off-topic part removed
Last edited by badidea on Apr 23, 2021 5:10, edited 1 time in total.
Re: Finer control of access within a Type ... Extends
I do not see how the above two responses have anything to do with my original question on an access type of protected for write but public for read that I can use for a library I am going to give to a client to use.
Wally
Wally
Re: Finer control of access within a Type ... Extends
One thing I still don't understand after a long time here is:
"Why asking questions here that are answered in full by the FB-Manual"
See: PROTECTED: (Access Control) (KeyPgVisProtected)
"Why asking questions here that are answered in full by the FB-Manual"
See: PROTECTED: (Access Control) (KeyPgVisProtected)
Re: Finer control of access within a Type ... Extends
The original poster is well aware of what "protected" does. But he's asking if there's a possible way to have a slightly different behavior. My read of the on-line manual suggests that, no there is not. I believe the OP will have to implement read and write access separately (two different property names) to get what he wants.
Re: Finer control of access within a Type ... Extends
Neither do I, indeed.wallyg wrote:I do not see how the above two responses have anything to do with my original question on an access type of protected for write but public for read that I can use for a library I am going to give to a client to use.
What you want is basically how Visual Basic treats variables within a class:
Code: Select all
Public Class Foo
Public Sub New()
'' We can still write to the variable from *within* the class
bar = -4
End Sub
'' This variable will be read-only from the client code (akin to a read-only property)
Public ReadOnly bar As Integer
End Class
Code: Select all
type Foo extends Object
public:
declare constructor()
declare property bar() as integer
protected:
as integer _bar
end type
constructor Foo()
_bar = -4
end constructor
property Foo.bar() as integer
return( _bar )
end property
Code: Select all
type Foo extends Object
public:
declare constructor()
declare property bar() as integer
protected:
declare property bar( as integer )
private:
as integer _bar
end type
constructor Foo()
_bar = -4
end constructor
property Foo.bar() as integer
return( _bar )
end property
property Foo.bar( value as integer )
_bar = value
end property
Re: Finer control of access within a Type ... Extends
I think ReadOnly in VB is like the Const Qualifier. And VB's ReadOnly can only be used in the constructor.
Here's a fragment to ponder:
Here's a fragment to ponder:
Code: Select all
type T
'' const qualifier is like VB's readonly
yyy as const integer '' const used like this makes yyy readonly
declare constructor()
end type
constructor T()
'' missing feature? maybe this should be allowed in a constructor?
'' yyy = 1 '' error: cannot modify a constant
'' cast away the constness and you're the boss!
*cptr( integer ptr, @this.yyy ) = 123
end constructor
dim x as T
print x.yyy '' accessing x.yyy -- OK
x.yyy = 666 '' error, cannot modify a constant
sleep
Re: Finer control of access within a Type ... Extends
Ah, indeed. However, it does allow use of the implicit internal variable if we define a readonly property:coderJeff wrote:I think ReadOnly in VB is like the Const Qualifier. And VB's ReadOnly can only be used in the constructor.
...
Code: Select all
Public Class Foo
Public ReadOnly Property Bar() As Integer
Public Sub Baz()
'' We can use the implicit variable behind the property
_Bar = 1234
End Sub
End Class
Re: Finer control of access within a Type ... Extends
A wotkaround to suppress the warning:coderJeff wrote:I think ReadOnly in VB is like the Const Qualifier. And VB's ReadOnly can only be used in the constructor.
Here's a fragment to ponder:Code: Select all
type T '' const qualifier is like VB's readonly yyy as const integer '' const used like this makes yyy readonly declare constructor() end type constructor T() '' missing feature? maybe this should be allowed in a constructor? '' yyy = 1 '' error: cannot modify a constant '' cast away the constness and you're the boss! *cptr( integer ptr, @this.yyy ) = 123 end constructor dim x as T print x.yyy '' accessing x.yyy -- OK x.yyy = 666 '' error, cannot modify a constant sleep
'CONST qualifier discarded'
induced by:
*cptr( integer ptr, @this.yyy ) = 123
(seen with compile option '-w constness' for example)
Use instead:
peek(integer, @this.yyy) = 123
Re: Finer control of access within a Type ... Extends
Thank you all for your insight. I did spend a significant amount of time, looking over all documentation and did extensive searches of the forum before posting on the forum. I was basically asking if I missed something somewhere to get the control I wanted. Obviously, I did not.
Therefore I was wondering if I could move this to the wish list for future versions of FB instead. Instead of the 3 options, Private, Protected, and Public we add another option for a variable to be Protected for write but Public for reading. If we are going that far then add one for Private for write, Public for reading, and one for Private for write, and Protected for reading just to complete the set.
Wally
Therefore I was wondering if I could move this to the wish list for future versions of FB instead. Instead of the 3 options, Private, Protected, and Public we add another option for a variable to be Protected for write but Public for reading. If we are going that far then add one for Private for write, Public for reading, and one for Private for write, and Protected for reading just to complete the set.
Wally
Re: Finer control of access within a Type ... Extends
What went wrong?badidea wrote:Less funny with the 32-bit compiler.dodicat wrote:For fun.
...
edit: off-topic part removed
Re: Finer control of access within a Type ... Extends
Sort of the same as coderJeff's however, without ptr access and const used:
Code: Select all
Type trial extends object
Private:
As Long _id_ ' read only (except: overloaded ctor)
Protected:
Declare Constructor() ' not usable throws error
Declare Sub init(ByVal As Long) ' setter protected (internal use only)
Public:
Declare Constructor(ByVal As Long) ' public copy-ctor
Declare Destructor() ' public dtor
Declare Property id() As Long ' public getter
End Type
Constructor trial() ' protected
End Constructor
Constructor trial(ByVal rhs As Long) ' public
This.init(rhs) ' allowed here (protected otherwise)
End Constructor
Destructor trial() ' public
End Destructor
Private Sub trial.init(ByVal rhs As Long) ' protected
This._id_ = rhs
End Sub
Property trial.id() As Long ' public
Return This._id_
End Property
'Dim As trial tri ' error 204: The default constructor has no public access in 'Dim As trial tri'
Dim As trial tri = Type<trial>(123) ' force overloaded constructor's use
Print tri.id
Sleep
Why then not asking that straight away???wallyg wrote:I was basically asking if I missed something somewhere to get the control I wanted.
Re: Finer control of access within a Type ... Extends
Unless my reading comprehension is way off, he did, right in the first post.MrSwiss wrote:Why then not asking that straight away???
Re: Finer control of access within a Type ... Extends
Nothing went horribly wrong, but the output of your code with 32-bit fbc here is:dodicat wrote:...
What went wrong?
Code: Select all
5.332916439231228e-315
1.911870621940413e+214
o
Bye, Press any key . . .