Passing Structures by address convention
Posted: Sat Dec 02, 2023 9:35 am
Hi,
I have noticed that structures can automatically be passed by address if needed without the use of '@'. I'm guessing this here to make the structures easier to work with?
The example in the documentation specifies the @ and it was only by accident that I discovered that it worked the same without. I discovered it when I had a pointer to a root structure with a nested structure inside and I needed the address of the nested structure.
I thought the above syntax looked a little strange and found I could simplify to.
Here is an example showing my experimentations with this convention.
I like the simplified look but the downside of it is it is not explicit that the structure is being passed by address. I thought I would check to see if this is valid Purebasic and not likely to break in the future?
I have noticed that structures can automatically be passed by address if needed without the use of '@'. I'm guessing this here to make the structures easier to work with?
The example in the documentation specifies the @ and it was only by accident that I discovered that it worked the same without. I discovered it when I had a pointer to a root structure with a nested structure inside and I needed the address of the nested structure.
Code: Select all
@*myBar\Foo
Code: Select all
*myBar\Foo
Code: Select all
Structure Foo
Val.l
EndStructure
Procedure UpdateFoo(*foo.Foo)
*foo\Val = Random(500)
EndProcedure
Define myFoo.Foo
; The following 2 lines work the same?
;UpdateFoo(myFoo)
UpdateFoo(@myFoo)
Debug "myFoo\Val = " + myFoo\Val
Structure Bar
Foo.Foo
EndStructure
Define *myBar.Bar = AllocateStructure(Bar)
; The following 2 lines work the same?
UpdateFoo(*myBar\Foo)
;UpdateFoo(@*myBar\Foo)
Debug "*myBar\Foo\Val = " + *myBar\Foo\Val
FreeStructure(*myBar)