Page 1 of 1

Legistics of declaring structures inside procedures?

Posted: Mon Mar 10, 2025 3:26 pm
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!

Re: Legistics of declaring structures inside procedures?

Posted: Mon Mar 10, 2025 8:10 pm
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.

Re: Legistics of declaring structures inside procedures?

Posted: Mon Mar 10, 2025 8:52 pm
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!