Passing Dynstructs as Parameters

As noted previously, the DYNSTRUCT data type acts essentially the same as X0, and so can be passed as a parameter or returned as the value of a function in all the same ways. The example above referenced a hypothetical function Fn’Get’Structure’Data$() which returned an instance of the specified DYNSTRUCT type. That function could be implemented something like the following:

Function Fn’Get’Structure’Data$(dyntype$ as T_DYN_NAME) as DYNSTRUCT

    If Fn'Dynst'Bind(dyntype$, Fn’Get’Structure’Data$) = 0 then

        <populate the Fn’Get’Structure’Data$ structure members>

    Endif

EndFunction

 

In the above case, the function assumes that the specified dynstruct name (dyntype$) has been defined, and it binds it to the function’s local, previously uninitialized dynstruct variable—i.e. the name of the function and its return value.

Note that to be realistic, the function would probably need to retrieve the structure layout information in order to determine how to populate the returned structure. See the previous example Retrieve Layout of a Dynstruct for details on that operation.

In another variation on the same theme, we could implement a similar function which receives an already-bound dynstruct variable, updating and returning it in place. In this case the return value of the function could be a status value…

Function Fn’Get’Structure’Data(dsx as DYNSTRUCT) as i2

    map1 dsname$,s,T_DYN_NAME

    dsname$ = Fn’Dynst’Name’From’Instance$(dsx)  ! get the Dynstruct name from instance

    if dsname$ = "" then

        ? "dsx not bound to a Dynstruct definition!"

    else

        <update fields of dsx>

        Xputarg @dsx              ! return updated dynstruct

    Endif

EndFunction

 

As in the previous case, the example has left to your imagination the details of how it will update or populate the structure about which the compiler and programmer may have known nothing in advance. But it would presumably involve retrieving the layout information for the dynstruct, and then using the structure name and member names and types to match the fields up with some other data.