Dim Byref syntax

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Dim Byref syntax

Post by dkl »

Yep that works, just like with byref function results.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:One case does not works:
- If we declare a base-type reference used to refer to a derived-type object, but the base-type having an abstract method!
=> error 305: UDT has unimplemented abstract methods
It's OK now after the fix:
dimbyref: Allow declaring references to UDTs with abstract methods
Last edited by fxm on Oct 06, 2015 11:28, edited 1 time in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Bug on defining a reference to a dynamic instance of an object having a private/protected destructor?

- At opposite of static instance of UDT, we can construct a dynamic instance of an UDT which has a private/protected Destructor (because its Destructor is not automatically called).
Example of compiling OK:

Code: Select all

Type UDT
  Private:
    Declare Destructor ()
    Dim As Integer i
End type
Destructor UDT ()
  Print "UDT.Destructor()"
End Destructor

Scope
  Dim As UDT Ptr pu = New UDT
End Scope

Sleep
- But we cannot define a reference to this dynamic instance (of an object with private/protected destructor).
However, when the reference will go out of scope, similarly the destructor will not be called automatically:

Code: Select all

Type UDT
  Private:
    Declare Destructor ()
    Dim As Integer i
End type
Destructor UDT ()
  Print "UDT.Destructor()"
End Destructor

Scope
  Dim As UDT Ptr pu = New UDT
  Dim Byref As UDT ru = *pu
End Scope

Sleep
Compiler output:
.....\FBIde0.4.6r4_fbc1.04.0\FBIDETEMP.bas(12) error 205: Destructor has no public access in 'Dim Byref As UDT ru = *pu'

Add exception for 'Dim Byref' compared to 'Dim' when the destructor is private/protected?
More generally, 'Dim Byref' must never trigger a compile error linked with Constructors/Destructor as 'Dim' does.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:More generally, 'Dim Byref' must never trigger a compile error linked with Constructors/Destructor as 'Dim' does.
No problem in that example in case of default constructor missing:

Code: Select all

Type UDT
  Declare Constructor (Byval i As Integer)
  Dim As Integer i
End type
Constructor UDT (Byval i As Integer)
  This.i = i
End Constructor

'Dim As UDT u  '' error 182: TYPE or CLASS has no default constructor
Dim As UDT u = 123
Dim Byref As UDT ru = u
Print ru.i

Sleep
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Dim Byref syntax

Post by dkl »

Thanks, this is fixed in Git now.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Principle of handling dynamic instances, through a pointer versus a reference

- To handle one dynamic instance:

Code: Select all

Type UDT
  Dim As String s = "..UDT non-static field"
  Declare Constructor ()
  Declare Destructor()
End Type
Constructor UDT (): Print "UDT constructor()", @This: End Constructor
Destructor UDT (): Print "UDT destructor", @This: End Destructor

Dim As UDT Ptr pu = New UDT
Print
Dim Byref As UDT ru = *New UDT
Print
Print pu->s, pu
Print
Print ru.s, @ru
Print
Delete pu
Print
Delete @ru

Sleep

Code: Select all

UDT constructor()           3351904

UDT constructor()           3352008

..UDT non-static field      3351904

..UDT non-static field      3352008

UDT destructor              3351904

UDT destructor              3352008
The pointer and the reference are both well adapted to handle one dynamic instance.

- To handle a collection of dynamic instances:

Code: Select all

Type UDT
  Dim As String s = "..UDT non-static field"
  Declare Constructor ()
  Declare Destructor()
End Type
Constructor UDT (): Print "UDT constructor()", @This: End Constructor
Destructor UDT (): Print "UDT destructor", @This: End Destructor

Dim As UDT Ptr pua = New UDT[3]
Print
Dim Byref As UDT rua = *New UDT[3]
Print
Print pua[0].s, @pua[0]: Print pua[1].s, @pua[1]: Print pua[2].s, @pua[2]
Print
Print (@rua)[0].s, @(@rua)[0]: Print (@rua)[1].s, @(@rua)[1]: Print (@rua)[2].s, @(@rua)[2]
Print
Delete[] pua
Print
Delete[] @rua

Sleep

Code: Select all

UDT constructor()           3351908
UDT constructor()           3351920
UDT constructor()           3351932

UDT constructor()           3352132
UDT constructor()           3352144
UDT constructor()           3352156

..UDT non-static field      3351908
..UDT non-static field      3351920
..UDT non-static field      3351932

..UDT non-static field      3352132
..UDT non-static field      3352144
..UDT non-static field      3352156

UDT destructor              3351932
UDT destructor              3351920
UDT destructor              3351908

UDT destructor              3352156
UDT destructor              3352144
UDT destructor              3352132
Only the pointer is well adapted to handle a collection of dynamic instances, except if a new specific operator is added for references, for example, operator {} defined by:
REF{n} = (@REF)[n]
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

- How is envisaged the initializer syntax for reference arrays?
Specially for big reference arrays!

- And for reference members, mandatory initialization only through the constructor (constructor initialization list as in C++)?
IMHO, it is not recommended to use reference members (use rather pointer members), except static reference members.
Syntax for static reference members to be defined/confirmed and allowed?

Code: Select all

Dim Shared As Integer i = 123

Type UDT
  Static Byref As Integer ri
  Dim As Integer dummy
End Type
Dim Byref As Integer UDT.ri = i

Print UDT.ri
Compiler output:
.....\FBIde0.4.6r4_fbc1.04.0\FBIDETEMP.bas(4) error 319: Reference not initialized in 'Static Byref As Integer ri'
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Why the last two code lines are not allowed?

Code: Select all

Dim As Integer Ptr pi
Dim As Any Ptr pa

Dim Byref As Integer Ptr rpi1 = pi
Dim Byref As Any Ptr rpa1 = pa
Dim Byref As Integer Ptr rpa2 = pa
Dim Byref As Any Ptr rpi2 = pi
Compiler output:
.....\FBIde0.4.6r4_fbc1.04.0\FBIDETEMP.bas(6) error 24: Invalid data types in 'Dim Byref As Integer Ptr rpa2 = pa'
.....\FBIde0.4.6r4_fbc1.04.0\FBIDETEMP.bas(7) error 24: Invalid data types in 'Dim Byref As Any Ptr rpi2 = pi'
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Dim Byref syntax

Post by dkl »

fxm wrote:- How is envisaged the initializer syntax for reference arrays?
Specially for big reference arrays!
FB's usual array initializer could be used:

Code: Select all

dim as integer a, b, c
dim byref array(0 to ...) as integer = { a, b, c }
It would be an array of references... but it can easily be confused with a reference to an array. Even more so in case of using such an array of references as function parameter.
fxm wrote:- And for reference members, mandatory initialization only through the constructor (constructor initialization list as in C++)?
IMHO, it is not recommended to use reference members (use rather pointer members), except static reference members.
Syntax for static reference members to be defined/confirmed and allowed?
Extern + Byref was broken (required an initializer) - fixed in Git now. That applies to static member variables too.
fxm wrote:Why the last two code lines are not allowed?
That's a bit too strict; it should be allowed as for parameters or Byval assignments. I've got a patch ready for this now, but since it's very intrusive I think it's better to wait until after the next release.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

dkl wrote:FB's usual array initializer could be used:

Code: Select all

dim as integer a, b, c
dim byref array(0 to ...) as integer = { a, b, c }
It would be an array of references... but it can easily be confused with a reference to an array. Even more so in case of using such an array of references as function parameter.
In order that this works also with polymorphism (and even with an abstract Base), this must be an array of Base-references and not a reference to a Base-array.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

dkl wrote:
fxm wrote:- And for reference members, mandatory initialization only through the constructor (constructor initialization list as in C++)?
IMHO, it is not recommended to use reference members (use rather pointer members), except static reference members.
Syntax for static reference members to be defined/confirmed and allowed?
Extern + Byref was broken (required an initializer) - fixed in Git now. That applies to static member variables too.
Ok, referencing works now for static member variables:

Code: Select all

Dim Shared As Integer i = 123

Type UDT
  Static Byref As Integer ri
  Dim As Integer dummy
End Type
Dim Byref As Integer UDT.ri = i

Print UDT.ri
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:
dkl wrote:FB's usual array initializer could be used:

Code: Select all

dim as integer a, b, c
dim byref array(0 to ...) as integer = { a, b, c }
It would be an array of references... but it can easily be confused with a reference to an array. Even more so in case of using such an array of references as function parameter.
In order that this works also with polymorphism (and even with an abstract Base), this must be an array of Base-references and not a reference to a Base-array.
Indeed, only array elements like pointers or references allow the polymorphism:

Code: Select all

'Macro simulating the declaration of an array of references
  #macro DimByRefArray (typeName, refName, lowBound, objectAddressList...)
    Dim As typeName Ptr ptr##refName(lowBound to ...) = { objectAddressList }
    #Define refName(n) (*ptr##refName(n))
  #endmacro

Type Parent Extends Object
    Declare Abstract Function rtnI () As Integer
End Type

Type Child1 Extends Parent
    Declare Virtual Function rtnI () As Integer Override
    Dim As Integer i = 111
End Type
Virtual Function Child1.rtnI () As Integer
    Return This.i
End Function

Type Child2 Extends Parent
    Declare Virtual Function rtnI () As Integer Override
    Dim As Integer i = 222
End Type
Virtual Function Child2.rtnI () As Integer
    Return This.i
End Function

Type Child3 Extends Parent
    Declare Virtual Function rtnI () As Integer Override
    Dim As Integer i = 333
End Type
Virtual Function Child3.rtnI () As Integer
    Return This.i
End Function

Dim As Child1 c1
Dim As Child2 c2
Dim As Child3 c3

'Dim Byref As Parent array(0 to 2) = { c1, c2, c3}
  DimByRefArray(Parent, array, 0, @c1, @c2, @c3)

For k As Integer = 0 to 2
    Print array(k).rtnI()
Next k
Print

Sleep
[edit]
- Added a macro fully simulating the declaration of an array of references.
Last edited by fxm on Oct 05, 2015 16:31, edited 3 times in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

fxm wrote:Ok, referencing works now for static member variables:

Code: Select all

Dim Shared As Integer i = 123

Type UDT
  Static Byref As Integer ri
  Dim As Integer dummy
End Type
Dim Byref As Integer UDT.ri = i

Print UDT.ri
In the definition line:
Dim Byref As Integer UDT.ri = i
'Byref' is not mandatory:

Code: Select all

Dim Shared As Integer i = 123

Type UDT
  Static Byref As Integer ri
  Dim As Integer dummy
End Type
Dim As Integer UDT.ri = i

Print UDT.ri
Is that normal or one checking is missing?
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Dim Byref syntax

Post by dkl »

Oh, looks like that's still missing.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Post Reply