type CHILDFWD as CHILD ptr
type BASETYPE extends OBJECT
declare abstract function F() as CHILDFWD
end type
type CHILD extends BASETYPE
'declare function F() as CHILDFWD
declare function F() as CHILD ptr
end type
'function CHILD.F() as CHILDFWD
function CHILD.F() as CHILD ptr
return @THIS
end function
C:\Program Files (x86)\FreeBasic\FBIde0.4.6r4\FBIDETEMP.bas(9) error 225: Override has different return type than overridden method in 'declare function F() as CHILD ptr'
Results:
Compilation failed
System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 1.06.0 (11-22-2018), built for win64 (64bit)
OS: Windows NT 6.2 (build 9200)
With operator CAST, the behaviour seems to be a life saver because it seems to allow returning value same as caller type. However I'm not sure why Cast doesn't allow this just by default ?
type UDTPTR as UDT ptr
type UDT extends OBJECT
declare operator CAST() as UDT ptr 'error 140: Parameter and result can't be of the same type, before ''' in 'declare operator CAST() as UDT ptr ''
declare operator CAST() as UDTPTR 'ok
end type
With normal procedural functions, overloaded functions are seperated by their parameter list, not return value.
With this abstract stuff the same seems to apply.
type CHILDFWD as CHILD ptr
type BASETYPE extends OBJECT
declare abstract function F(as ulong) as CHILDFWD
end type
type CHILD extends BASETYPE
'declare function F() as CHILDFWD
declare function F(as long) as CHILD ptr
end type
'function CHILD.F() as CHILDFWD
function CHILD.F(dummy as long) as CHILD ptr
return @THIS
end function
sleep
So put dummy parameters in so not to cause ambiguoustoy.
Who knows, some day you might even use them.
type CHILDFWD as CHILD ptr
type BASETYPE extends OBJECT
declare abstract function F() as CHILDFWD
end type
type CHILD extends BASETYPE
'declare function F() as CHILDFWD
declare function F() as CHILDFWD
end type
'function CHILD.F() as CHILDFWD
function CHILD.F() as CHILD ptr
return @THIS
end function
Last edited by fxm on Jan 11, 2019 15:30, edited 1 time in total.
Is there any comment on the case of CAST? I can also make some real testing. But just in case someone already knows? Because here the problem is exactly the opposite as the previous one.
This doesn't work until the end but it's almost it:
type UDTPTR as UDT ptr
type UDT extends OBJECT
'declare operator CAST() as UDT '
declare operator CAST() as UDTPTR
end type
operator UDT.CAST() as UDTPTR
return @THIS
end operator
error 140: Parameter and result can't be of the same type in 'operator UDT.CAST() as UDTPTR'
Results:
Compilation failed
System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 1.06.0 (11-22-2018), built for win64 (64bit)
OS: Windows NT 6.2 (build 9200)
Thanks.
You don't need any parameters after all.
Sorry.
I get an error with the operators in your first post.
...
Parameter and result can't be of the same type
Type UDT
Dim As Integer I
Declare Operator Cast () As UDT
Declare Operator Cast () As UDT Ptr
End Type
The compiler forbids this type of return to the Cast operator to help the user avoid the problem caused by an infinite loop of calls.
But because of the previous bug (# 672), the compiler thinks the type is different.
error 140: Parameter and result can't be of the same type
In this case, the parameter is the parameter 'This' passed in a hidden way.
Last edited by fxm on Jan 11, 2019 15:53, edited 3 times in total.
dodicat wrote:
I get an error with the operators in your first post.
With Cast , it's not exactly the same question I was trying to get some Cast to that returns the instance itself. It's not an obvious task, this below doesn't work with a function:
Type UDT
Dim As Integer I
Declare Operator Cast () As UDT
Declare Operator Cast () As UDT Ptr
End Type
The compiler forbids this type of return to the Cast operator to help the user avoid the problem caused by an infinite loop of calls.
But because of the previous bug (# 672), the compiler thinks the type is different.
Ok I get it now.
So it will end in a feature request. Couldn't we have a fake Cast that returns the instance (and hides the operator as usual) when we use this syntax. That would of course not be same as returning a differnt type, but hidding the operator at call time is quite nice.
type UDT extends OBJECT
declare function SELF() byref as UDT
end type
function UDT.SELF() byref as UDT
return THIS
end function
dim as UDT uuu
? @(uuu.SELF())
type UDT extends OBJECT
declare function SELF() byref as UDT
end type
function UDT.SELF() byref as UDT
return THIS
end function
dim as UDT uuu
? @(uuu.SELF())
I must admit that even if I would eventually find the answer myself now with the experience, I still find that there is here nothing obvious.
For me the THIS variable is something that implies something equivalent to SELF(byref THIS as UDT=THIS). (<--- the actual value of THIS is passed so)
In your last explanation, it seems more like:
- When returning by value, 'Return This' returns a temporary copy of the reference 'This'.
- When returning by reference, 'Return This' returns the reference 'This' (under the hood, a pointer to 'This' with is automatically dereferenced).
fxm wrote:- When returning by value, 'Return This' returns a temporary copy of the reference 'This'.
- When returning by reference, 'Return This' returns the reference 'This' (under the hood, a pointer to 'This' with is automatically dereferenced).
I'm not sure that the term is well understood as something related to the BYREF page for someone reading about THIS. At least for me the link doesn't jump to my eyes.
The doc says:
This is a reference to an instance of a Type or Class that is passed as a hidden argument
Maybe some link to the BYREF page could be inserted just here so that all the words may be attached to this context of argument passed by reference, understand byref. Just a suggestion of course. Could be an example otherwise?
This is a reference to an instance of a Type or Class that is passed (see also BYREF for generalities) as a hidden argument