type A extends object
i as Long
End Type
function Test() as A
dim p as A
p.i = 10
if 1 then
p.i = 20
function = p
EndIf
return p
End function
dim p as A = Test()
? p.i
error 178: RETURN mixed with 'FUNCTION =' or EXIT FUNCTION (using both styles together is unsupported when returning objects with constructors), found 'p' in 'return p'
In case the object to be returned (by value) needs to be actually constructed:
- 'FUNTION' calls the default constructor once, then the copy-let operator at each possible value of 'FUNCTION =' (so the last value wins).
- The first 'RETURN' encountered calls the copy constructor (and the object is immediately returned).
So there can be no concurrency of these 2 syntaxes that would therefore induce 2 distinct objects.
Workaround:
- 'RETURN p' can easily be replaced by 'FUNCTION = p : EXIT FUNCTION'.
- Using the copy constructor only instead of the default constructor plus the copy-let operator, 'RETURN p' can be considered as an optimization of 'FUNCTION = p : EXIT FUNCTION'.
Note: 'RETURN' and 'FUNCTION =' can be mixed regardless of the object when returned by reference.