Finer control of access within a Type ... Extends

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

Finer control of access within a Type ... Extends

Postby wallyg » Apr 20, 2021 18:46

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?
dodicat
Posts: 6914
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Finer control of access within a Type ... Extends

Postby dodicat » Apr 22, 2021 16:10

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
 
badidea
Posts: 2303
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Finer control of access within a Type ... Extends

Postby badidea » Apr 22, 2021 22:23

dodicat wrote:For fun.
...

Less funny with the 32-bit compiler.

edit: off-topic part removed
Last edited by badidea on Apr 23, 2021 5:10, edited 1 time in total.
wallyg
Posts: 127
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Finer control of access within a Type ... Extends

Postby wallyg » Apr 22, 2021 22:48

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
MrSwiss
Posts: 3796
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Finer control of access within a Type ... Extends

Postby MrSwiss » Apr 22, 2021 23:05

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)
caseih
Posts: 1666
Joined: Feb 26, 2007 5:32

Re: Finer control of access within a Type ... Extends

Postby caseih » Apr 22, 2021 23:19

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.
paul doe
Moderator
Posts: 1414
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Finer control of access within a Type ... Extends

Postby paul doe » Apr 22, 2021 23:31

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.

Neither do I, indeed.

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

But you're out of luck with FreeBasic. The only way to do it is via property:

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

Or alternatively, if you don't want to expose the variable directly to derived classes:

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
coderJeff
Site Admin
Posts: 3484
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Finer control of access within a Type ... Extends

Postby coderJeff » Apr 23, 2021 2:22

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
paul doe
Moderator
Posts: 1414
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Finer control of access within a Type ... Extends

Postby paul doe » Apr 23, 2021 3:04

coderJeff wrote:I think ReadOnly in VB is like the Const Qualifier. And VB's ReadOnly can only be used in the constructor.
...

Ah, indeed. However, it does allow use of the implicit internal variable if we define a readonly property:

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

Which in this case is equivalent to the first snippet I posted...
fxm
Moderator
Posts: 10284
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Finer control of access within a Type ... Extends

Postby fxm » Apr 23, 2021 5:31

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

A wotkaround to suppress the warning:
'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
wallyg
Posts: 127
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Finer control of access within a Type ... Extends

Postby wallyg » Apr 23, 2021 6:04

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
dodicat
Posts: 6914
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Finer control of access within a Type ... Extends

Postby dodicat » Apr 23, 2021 7:40

badidea wrote:
dodicat wrote:For fun.
...

Less funny with the 32-bit compiler.

edit: off-topic part removed

What went wrong?
MrSwiss
Posts: 3796
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Finer control of access within a Type ... Extends

Postby MrSwiss » Apr 23, 2021 15:02

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

wallyg wrote:I was basically asking if I missed something somewhere to get the control I wanted.
Why then not asking that straight away???
caseih
Posts: 1666
Joined: Feb 26, 2007 5:32

Re: Finer control of access within a Type ... Extends

Postby caseih » Apr 23, 2021 18:51

MrSwiss wrote:Why then not asking that straight away???
Unless my reading comprehension is way off, he did, right in the first post.
badidea
Posts: 2303
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Finer control of access within a Type ... Extends

Postby badidea » Apr 23, 2021 21:35

dodicat wrote:...
What went wrong?
Nothing went horribly wrong, but the output of your code with 32-bit fbc here is:

Code: Select all

 5.332916439231228e-315
 1.911870621940413e+214
o
Bye, Press any key . . .

Return to “General”

Who is online

Users browsing this forum: No registered users and 17 guests