Returning multiple outputs with get #1

Open
opened 2025-10-11 01:05:28 +00:00 by UserCurt · 1 comment
UserCurt commented 2025-10-11 01:05:28 +00:00 (Migrated from codeberg.org)

It can be useful to return multiple values simultaneously, for simultaneous assignment for example.

S = struct(A=1, B=2);
[x,y] = mpath.get(S, "/*")

The means to indicate when or how multiple outputs is handled is not yet determined. There are a couple options:

Named Argument: One option is to have a named argument that says which dimension(s) will be collapsed and used for multiple outputs. What name to use is not yet determined. This is nice because it does not require changing syntax. But it can be confusing because automatically assigned dimensions may be unclear. Perhaps this should only work with manually applied dimensions.

New Syntax: Alternatively, new syntax can be introduced. Two contenders are to modify the dimension specifier notation to follow by a keyword: mpath.get(S, "/*#list"). The term "list" is used to concisely mirror Mathwork's usage of the phrase comma separated list while not being verbose or using abbreviations. Alternatively, the # symbol can be repeated: mpath.get(S, "/*##"). In my opinion, this looks weird and could be misconstrued as a typo.

If introducing new syntax, it is unclear how this will interact with mpath.resolve, mpath.set, mpath.remove, mpath.exist. What behavior would be expected? Should it give an error? Should it act just like any other dimension?

Ideally, there would be a mechanism for the order in which the outputs are selected, but this isn't critical for the feature. If using a named argument, this could be determined by the order of the dimensions. Here, I use OutputDims as the named argument. A few examples:

S = struct(A=struct(X=1, Y=2), B=struct(X=3, Y=4));
[p,q,r,s] = mpath.get(S, "/*#1/*#1", OutputDims=1) % is this [S.A.X, S.A.Y, S.B.X, S.B.Y] or [S.A.X, S.B.X, S.A.Y, S.B.Y]
[p,q,r,s] = mpath.get(S, "/*#1/*#2", OutputDims=[1 2])
[p,q,r,s] = mpath.get(S, "/*#1/*#2", OutputDims=[2 1])
It can be useful to return multiple values simultaneously, for simultaneous assignment for example. ```matlab S = struct(A=1, B=2); [x,y] = mpath.get(S, "/*") ``` The means to indicate when or how multiple outputs is handled is not yet determined. There are a couple options: **Named Argument:** One option is to have a named argument that says which dimension(s) will be collapsed and used for multiple outputs. What name to use is not yet determined. This is nice because it does not require changing syntax. But it can be confusing because automatically assigned dimensions may be unclear. Perhaps this should only work with manually applied dimensions. **New Syntax:** Alternatively, new syntax can be introduced. Two contenders are to modify the dimension specifier notation to follow by a keyword: `mpath.get(S, "/*#list")`. The term "list" is used to concisely mirror Mathwork's usage of the phrase [comma separated list](https://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html) while not being verbose or using abbreviations. Alternatively, the `#` symbol can be repeated: `mpath.get(S, "/*##")`. In my opinion, this looks weird and could be misconstrued as a typo. If introducing new syntax, it is unclear how this will interact with `mpath.resolve`, `mpath.set`, `mpath.remove`, `mpath.exist`. What behavior would be expected? Should it give an error? Should it act just like any other dimension? Ideally, there would be a mechanism for the order in which the outputs are selected, but this isn't critical for the feature. If using a named argument, this could be determined by the order of the dimensions. Here, I use `OutputDims` as the named argument. A few examples: ```matlab S = struct(A=struct(X=1, Y=2), B=struct(X=3, Y=4)); [p,q,r,s] = mpath.get(S, "/*#1/*#1", OutputDims=1) % is this [S.A.X, S.A.Y, S.B.X, S.B.Y] or [S.A.X, S.B.X, S.A.Y, S.B.Y] [p,q,r,s] = mpath.get(S, "/*#1/*#2", OutputDims=[1 2]) [p,q,r,s] = mpath.get(S, "/*#1/*#2", OutputDims=[2 1]) ```
UserCurt commented 2025-10-11 01:12:19 +00:00 (Migrated from codeberg.org)

This could also be used in mpath.resolve:

S = struct(A=struct(X=1, Y=2), B=struct(X=3, Y=4));
[p,q] = mpath.resolve(S, "/*#1/*#2", OutputDims=2) % p = ["/A/X"; "/B/X"], q = ["/A/Y"; "/B/Y"]

Regarding mpath.set, this could be considered as the sole means for simultaneous assignment, akin to comma separated list assignments. Though there could be accidental assignments when working with heterogeneous data that happen to have the correct number of items in the comma separated list. For example, you can have 6 outputs equally from 2×3 or 3×2, and since the assignment wouldn't really have a shape, there is no error checking. Though with OutputDims, there could be a theoretical "shape" to this that could be checked.

This could also be used in `mpath.resolve`: ```matlab S = struct(A=struct(X=1, Y=2), B=struct(X=3, Y=4)); [p,q] = mpath.resolve(S, "/*#1/*#2", OutputDims=2) % p = ["/A/X"; "/B/X"], q = ["/A/Y"; "/B/Y"] ``` Regarding `mpath.set`, this could be considered as the sole means for simultaneous assignment, akin to [comma separated list assignments](https://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html#br2js35-7). Though there could be accidental assignments when working with heterogeneous data that happen to have the correct number of items in the comma separated list. For example, you can have 6 outputs equally from 2×3 or 3×2, and since the assignment wouldn't really have a shape, there is no error checking. Though with `OutputDims`, there could be a theoretical "shape" to this that could be checked.
Sign in to join this conversation.
No description provided.