Dim Byref syntax
Re: Dim Byref syntax
Yep that works, just like with byref function results.
Re: Dim Byref syntax
It's OK now after the fix: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
dimbyref: Allow declaring references to UDTs with abstract methods
Last edited by fxm on Oct 06, 2015 11:28, edited 1 time in total.
Re: Dim Byref syntax
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:
- 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: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.
- 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
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
.....\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.
Re: Dim Byref syntax
No problem in that example in case of default constructor missing:fxm wrote:More generally, 'Dim Byref' must never trigger a compile error linked with Constructors/Destructor as 'Dim' does.
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
Re: Dim Byref syntax
Thanks, this is fixed in Git now.
Re: Dim Byref syntax
Principle of handling dynamic instances, through a pointer versus a reference
- To handle one dynamic instance:The pointer and the reference are both well adapted to handle one dynamic instance.
- To handle a collection of dynamic instances: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]
- 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
- 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
REF{n} = (@REF)[n]
Re: Dim Byref syntax
- 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?Compiler output:
.....\FBIde0.4.6r4_fbc1.04.0\FBIDETEMP.bas(4) error 319: Reference not initialized in 'Static Byref As Integer ri'
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
.....\FBIde0.4.6r4_fbc1.04.0\FBIDETEMP.bas(4) error 319: Reference not initialized in 'Static Byref As Integer ri'
Re: Dim Byref syntax
Why the last two code lines are not allowed?
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'
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
.....\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'
Re: Dim Byref syntax
FB's usual array initializer could be used:fxm wrote:- How is envisaged the initializer syntax for reference arrays?
Specially for big reference arrays!
Code: Select all
dim as integer a, b, c
dim byref array(0 to ...) as integer = { a, b, c }
Extern + Byref was broken (required an initializer) - fixed in Git now. That applies to static member variables too.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?
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 wrote:Why the last two code lines are not allowed?
Re: Dim Byref syntax
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.dkl wrote:FB's usual array initializer could be used: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.Code: Select all
dim as integer a, b, c dim byref array(0 to ...) as integer = { a, b, c }
Re: Dim Byref syntax
Ok, referencing works now for static member variables:dkl wrote:Extern + Byref was broken (required an initializer) - fixed in Git now. That applies to static member variables too.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?
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
Re: Dim Byref syntax
Indeed, only array elements like pointers or references allow the polymorphism:fxm wrote: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.dkl wrote:FB's usual array initializer could be used: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.Code: Select all
dim as integer a, b, c dim byref array(0 to ...) as integer = { a, b, c }
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
- 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.
Re: Dim Byref syntax
In the definition line: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
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
Re: Dim Byref syntax
Oh, looks like that's still missing.
Re: Dim Byref syntax
Thanks, for the fix:
Require/disallow Byref if Dim'ing an Extern
Require/disallow Byref if Dim'ing an Extern