ABSTRACT doesn't allow type aliases?

General FreeBASIC programming questions.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

ABSTRACT doesn't allow type aliases?

Postby Tourist Trap » Jan 11, 2019 15:14

Hello, is this a know behaviour? FBC can't recognize a simple type alias when using ABSTRACT:

Code: Select all

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 ?

Code: Select all


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


Thanks
dodicat
Posts: 5892
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: ABSTRACT doesn't allow type aliases?

Postby dodicat » Jan 11, 2019 15:24

With normal procedural functions, overloaded functions are seperated by their parameter list, not return value.
With this abstract stuff the same seems to apply.

Code: Select all

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.
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 15:27

Problem already identified, impacting overloading and overriding:
#672 Forward declaration considered as different type in signature of procedure

Workaround:

Code: Select all

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.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Postby Tourist Trap » Jan 11, 2019 15:29

fxm wrote:Problem already identified, impacting overloading and overriding:
#672 Forward declaration considered as different type in signature of procedure

Thanks fxm.

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:

Code: Select all

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)
dodicat
Posts: 5892
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: ABSTRACT doesn't allow type aliases?

Postby dodicat » Jan 11, 2019 15:38

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
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 15:49

Code: Select all

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.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Postby Tourist Trap » Jan 11, 2019 15:51

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:

Code: Select all

type UDT extends OBJECT
    declare function SELF() as UDT
end type

function UDT.SELF() as UDT
    return THIS
end function

dim as UDT  uuu

? @(uuu.SELF())


fxm wrote:

Code: Select all

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.
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 16:01

To obtain a valid address, you must return a reference (and not a temporary variable):

Code: Select all

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())
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Postby Tourist Trap » Jan 11, 2019 16:39

fxm wrote:To obtain a valid address, you must return a reference (and not a temporary variable):

Code: Select all

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:

Code: Select all

function SELF() as UDT
   dim THIS as UDT  'local, arbitrary value of THIS
   return THIS
end function

That seems not compatible. So I find it difficult to get the logic behind.
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 16:50

- 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).
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Postby Tourist Trap » Jan 11, 2019 17:02

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).

Thanks fxm. Is it internal or do you think this makes sense to talk about this at the doc page?
https://www.freebasic.net/wiki/wikka.ph ... =KeyPgThis
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 17:13

But this is true for any reference (no only 'This').

See BYREF (function results)
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Postby Tourist Trap » Jan 11, 2019 17:36

fxm wrote:But this is true for any reference (no only 'This').

See BYREF (function results)

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
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 17:54

But the page to link should be:
BYREF (parameters)
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Postby fxm » Jan 11, 2019 19:17

KeyPgThis → fxm [Added a link to BYREF (parameters) in the Description paragraph]

Return to “General”

Who is online

Users browsing this forum: No registered users and 9 guests