Dim Byref syntax

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

dodicat wrote:Munair
I can compile your code OK, but to read it I have to get yourcode.pp.bas
I am unable to mentally inline you macros without incurring a headache.
OK I get it...
fxm
Moderator
Posts: 12085
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Munair wrote:when a reference/instance is created, the (default) constructor is called along with everything that needs to be initialized
- When an object instance is created, a constructor (if exists) is called.
- Then when a reference to this instance is created, only its internal pointer (implicitly dereferenced by compiler when reference is used) is initialized with the instance address value: so, no constructor is called.
Last edited by fxm on Nov 27, 2018 15:37, edited 1 time in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

@dodicat:

Code: Select all

type TFolderItem extends object
 filename as string
 folder as string
 declare constructor()
 declare destructor()
end type

declare function GetSaveFolderItem() byref as TFolderItem

constructor TFolderItem()
 print "default constructor called"
end constructor

destructor TFolderItem()
 print "destructor called"
end destructor

function GetSaveFolderItem() byref as TFolderItem
 dim byref f as TFolderItem = *cptr(TFolderItem ptr, 0)
 dim confirmed as boolean = false

 if confirmed then
 @(f) = @(TFolderItem())
 end if

 return f
end function

dim byref f as TFolderItem = *cptr(TFolderItem ptr, 0)

 @(f) = @(GetSaveFolderItem())
if not @(f) =0 then
 print "file saved"
else
 print "file not saved, action aborted."
end if

end
fxm
Moderator
Posts: 12085
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

dodicat wrote:Munair
I am unable to mentally inline you macros without incurring a headache.
@Munair,
Me too.
To understand your code, I must replace in the source code the calls to macros by their definitions.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

fxm wrote:
Munair wrote:when a reference/instance is created, the (default) constructor is called along with everything that needs to be initialized
- When an object instance is created, a constructor (if exists) is called.
- Then when a reference to this instance is created, only its internal pointer (implicitly dereferenced by compiler whenreference is used) is initialized with the instance address value: so, no constructor is called.
I do not mean a reference TO an instance. By reference/instance I mean the identifier that points to the copy of the object structure like myObj = new TMyObj whereby myObj is the reference to or instance of TMyObj. Perhaps the word reference in this context is confusing.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

fxm wrote:
dodicat wrote:Munair
I am unable to mentally inline you macros without incurring a headache.
@Munair,
Me too.
To understand your code, I must replace in the source code the calls to macros by their definitions.
See my previous post that I just provided Dodicat with.
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Dim Byref syntax

Post by St_W »

Munair wrote:This will never compile nor execute because one is not allowed to touch the child reference [...]
But it can be easily fixed. Just note that the new operator returns a pointer in FB.

Code: Select all

type tOne extends object
   s as string
   declare constructor()
end type

constructor tOne()
end constructor

type tTwo extends object
   s as string
   One as TOne ptr
   declare constructor()
end type

constructor tTwo()
   One = new tOne
   delete One
   One = 0
end constructor

dim two as tTwo
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

Yes, it can be easily fixed. As I pointed out earlier, the only way to do it is by directly using pointers. However, this is not very BASIC like, it is more C like. The inevitable syntax like Two.One->s = "" requires the programmer to know what is what. This can be complicated and a pain in large projects, even for seasoned programmers. QuickBASIC and later also RealBASIC were popular because they did their best not to force the programmer to use pointers. OOP can be complicated as it is. Forcing / encouraging the use of pointers along the way doesn't exactly make things easier.
fxm
Moderator
Posts: 12085
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Code: Select all

type tOne extends object
   s as string
   declare constructor()
end type

constructor tOne()
end constructor

type tTwo extends object
   s as string
   One as TOne ptr
end type


dim two as tTwo
two.One = New tOne

two.One->s = "FreeBASIC"
print "'" & two.One->s & "'"

two.One->destructor()
print "'" & two.One->s & "'"

delete two.one
sleep
Or:
two.One->s = tOne().s
to only reset 's'.
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Dim Byref syntax

Post by St_W »

Munair wrote:[...] As I pointed out earlier, the only way to do it is by directly using pointers. However, this is not very BASIC like, it is more C like. [...]
I agree that it is - in general many features that were added to the initial feature-set derived from QBasic are inspired by C or C++. That was the way the compiler developers went and can be hardy changed now without causing lots of compatibility issues with existing source code. However, it might be possible to change the behaviour for newly added features. For example if classes (using the reserved class keyword) are added one day they could use reference semantics like you described.
The FB language as it is now simply wasn't designed to allow null references and thus it works only with ugly workarounds, at best.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

St_W wrote:However, it might be possible to change the behaviour for newly added features. For example if classes (using the reserved class keyword) are added one day they could use reference semantics like you described.
viewtopic.php?f=17&t=23584&start=105#p255056
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

Maybe I forgot, or it is still not entirely clear to me. See the following example:

Code: Select all

type TT extends object
	a as integer
end type

function InitTT() byref as TT
	dim byref t as TT = *new TT
	t.a = 5
	return t
end function

dim t as TT = InitTT
print t.a
' delete @t ???
I'm not sure how to delete an object instantiated with *new. It looks like the compiler doesn't accept the data type with the delete statement.
fxm
Moderator
Posts: 12085
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

't' is only a static copy of the dynamic instance 'InitTT()' that you want to delete. So delete 't' is disallowed.
Instead, create a reference 't' to 'InitTT()':

Code: Select all

type TT extends object
   a as integer
end type

function InitTT() byref as TT
   dim byref t as TT = *new TT
   t.a = 5
   return t
end function

dim byref t as TT = InitTT
print t.a
delete @t
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Dim Byref syntax

Post by Munair »

So I take it that the static copy will be deleted when it loses scope?
fxm
Moderator
Posts: 12085
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dim Byref syntax

Post by fxm »

Exactly.
Post Reply