Legistics of declaring structures inside procedures?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Legistics of declaring structures inside procedures?

Post by Quin »

In my application, I handle many different types of packets. As such, I define each packet structure inside the case block that needs it inside my HandleMessage() procedure, because the structures are only needed in this one specific instance. For example:

Code: Select all

	Select Type
		Case #PacketType_Connection
			Structure ConnectionPacket
				Name$
				OS.i
			EndStructure
			Protected Connection.ConnectionPacket
			ExtractJSONStructure(JSONValue(ID), @Connection, ConnectionPacket)
			Users(Str(ClientID))\Name$ = Connection\Name$
			Users(Str(ClientID))\OS$ = OSVersionString(Connection\OS)
			Users(Str(ClientID))\ID = ClientID
My question is, how recommended is this, and are there any drawbacks? I know I've seen this discussed here before, but "structure inside procedure" is an unfortunately unspecific search. It compiles and works just fine, but I'm not sure how this actually works under the hood. Are they actually declared in the top-level like any structures not inside a procedure?
Thanks!
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Legistics of declaring structures inside procedures?

Post by Demivec »

Quin wrote: Mon Mar 10, 2025 3:26 pm Are they actually declared in the top-level like any structures not inside a procedure?
They are all 'global' structures and can be referenced after their declaration both inside and outside procedures. The only limitations in this regard is the one that exists in connection with module-scope. Those limitations are the boundary between inside or outside a module and also whether something is publicly or privately accessible within a module.

In all cases, on their own procedures do not create a separate scope with regard to structures, constants, or enumerations.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Legistics of declaring structures inside procedures?

Post by Quin »

Demivec wrote: Mon Mar 10, 2025 8:10 pm They are all 'global' structures and can be referenced after their declaration both inside and outside procedures. The only limitations in this regard is the one that exists in connection with module-scope. Those limitations are the boundary between inside or outside a module and also whether something is publicly or privately accessible within a module.

In all cases, on their own procedures do not create a separate scope with regard to structures, constants, or enumerations.
Awesome, understood. Thanks for the explanation!
Post Reply