OK I get it...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.
Dim Byref syntax
Re: Dim Byref syntax
Re: Dim Byref syntax
- When an object instance is created, a constructor (if exists) is called.Munair wrote:when a reference/instance is created, the (default) constructor is called along with everything that needs to be initialized
- 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.
Re: Dim Byref syntax
@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
Re: Dim Byref syntax
@Munair,dodicat wrote:Munair
I am unable to mentally inline you macros without incurring a headache.
Me too.
To understand your code, I must replace in the source code the calls to macros by their definitions.
Re: Dim Byref syntax
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.fxm wrote:- When an object instance is created, a constructor (if exists) is called.Munair wrote:when a reference/instance is created, the (default) constructor is called along with everything that needs to be initialized
- 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.
Re: Dim Byref syntax
See my previous post that I just provided Dodicat with.fxm wrote:@Munair,dodicat wrote:Munair
I am unable to mentally inline you macros without incurring a headache.
Me too.
To understand your code, I must replace in the source code the calls to macros by their definitions.
Re: Dim Byref syntax
But it can be easily fixed. Just note that the new operator returns a pointer in FB.Munair wrote:This will never compile nor execute because one is not allowed to touch the child reference [...]
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
Re: Dim Byref syntax
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.
Re: Dim Byref syntax
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
two.One->s = tOne().s
to only reset 's'.
Re: Dim Byref syntax
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.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. [...]
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.
Re: Dim Byref syntax
viewtopic.php?f=17&t=23584&start=105#p255056St_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.
Re: Dim Byref syntax
Maybe I forgot, or it is still not entirely clear to me. See the following example:
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.
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 ???
Re: Dim Byref syntax
'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()':
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
Re: Dim Byref syntax
So I take it that the static copy will be deleted when it loses scope?
Re: Dim Byref syntax
Exactly.