HalfBug (incosistency??) with returning UDTs

General FreeBASIC programming questions.
Post Reply
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

HalfBug (incosistency??) with returning UDTs

Post by Mysoft »

Code: Select all

Type MyUDT
  as integer X,Y
 end type
 
function RegularReturn() as integer
   RegularReturn = 10
   function = 10
   return 10 'all fine
 end function
 
 function UDTReturn() as MyUDT   
   
   UDTReturn =  type<MyUDT>(10,20) 'fine
 
   'function.x = 10 'illegal inside functions find function
   'UDTReturn.x = 10 'calls the function recursively infinitely :D because () is optional for sub/functions 'MEH
   'with function 'expected identifier, found 'function' in 'with function
   'with UDTReturn 'calls the function recursively infinitely :D because () is optional for sub/functions 'MEH   
   return type<MyUDT>(10,20) 'fine
   
end function

sub UDTReturn2( byref RetUDT as MyUDT )
  RetUDT.x = 10 : RetUDT.y = 20 'fine  
 end sub
 
 dim as MyUDT T
T = UDTReturn()
UDTReturn2( T )
 
so we can't handle the returned UDT members direclty it seems, i first tought that was a bug (because on freebasic 1.01 the WITH was giving an assembly error), but the -gen gcc of that was crashing just like the 1.05 (so that got fixed already) and so i quickly realized it was just infinite recursive call

and the method using the template requires to fill the whole type at once, and as far i know that creates a temp... that is then copied to the return that is then copied to the T (probabily gen gcc would optimize that but still)

the syntax is incosistent when an UDT is returned... this was discussed before? i couldnt find a bug filled about that, after all it isnt what i would an actual bug. (and i never noticed before because i never return UDT, just pointers)

so what you guys think?
aloberoger
Posts: 507
Joined: Jan 13, 2009 19:23

Re: HalfBug (incosistency??) with returning UDTs

Post by aloberoger »

what about returning byref AS UDT ?
Xusinboy Bekchanov
Posts: 783
Joined: Jul 26, 2018 18:28

Re: HalfBug (incosistency??) with returning UDTs

Post by Xusinboy Bekchanov »

You can do that, too:

Code: Select all

Type MyUDT
	As Integer X,Y
End Type

Function RegularReturn() As Integer
	RegularReturn = 10
	Function = 10
	Return 10 'all fine
End Function

Function UDTReturn() As MyUDT
	
	UDTReturn =  Type < MyUDT > (10, 20) 'fine
	Dim As MyUDT UDT
	UDT.X = 10
	UDT.Y = 20
	'Function.x = 10 'illegal inside functions find function
	'UDTReturn.x = 10 'calls the function recursively infinitely :D because () is optional for sub/functions 'MEH
	'with function 'expected identifier, found 'function' in 'with function
	'with UDTReturn 'calls the function recursively infinitely :D because () is optional for sub/functions 'MEH
	'Return Type<MyUDT>(10,20) 'fine
	Return UDT 'fine
	
End Function

Sub UDTReturn2( ByRef RetUDT As MyUDT )
	RetUDT.x = 10 : RetUDT.y = 20 'fine
End Sub

Dim As MyUDT T
T = UDTReturn()
UDTReturn2( T )
Sleep
erik
Posts: 39
Joined: Dec 28, 2020 17:27
Location: Krasnoyarsk
Contact:

Re: HalfBug (incosistency??) with returning UDTs

Post by erik »

Bikoz the "function" is not an expression of type "lvalue". Function are "rvalue".
You should create a variable.
Post Reply