[SUGGESTION] Object de-structuring for function declarations #856
Closed
alexgb0
started this conversation in
Suggestions
Replies: 3 comments
-
It looks like you're not following C.1: Organize related data into structures ( |
Beta Was this translation helpful? Give feedback.
0 replies
-
You're limiting the use of your functions to only work with that struct, so any generic use for your function is pointles. So either your function needs to be a member of the struct, (because it genuinely has no generic reusability outside of it) or you're going to have hacks where people create the whole struct unnecessarily just to get the function to work with the values they wanted to run it with anyway.
As mentioned before, composition is possibly the data pattern you're looking for.
On 25 November 2023 02:23:35 Johel Ernesto Guerrero Peña ***@***.***> wrote:
It looks like you're not following C.1: Organize related data into structures (structs or classes)<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-org>.
If you did, those x and y would be in some "point" abstraction.
That's much more type safe than doing type-safety patch-works with destructuring in function signatures.
—
Reply to this email directly, view it on GitHub<#856 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQJQ4HMC3JK2GYH3AW3YGFJCJAVCNFSM6AAAAAA7ZVGDISVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TMNRUGQ2TO>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks! It sounds like this this is answered, closing... I'm happy to reopen it if there's more. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Motivation
While doing data oriented design, sometimes you can end up with large datatype (almost always is going to be structs) with a lot of information.
Now, if you are making a function that only attacks some members, you end up declaring functions with the data you need
void some_calc(int x, int y, etc...)
. Not only, this sometimes can be a problem, but it's quite annoying to call themsome_calc(fig.x, fig.y, etc...)
.This is annoying but also, imagine you are writing a game engine and there is a function that takes the
x
andy
of a datatype. Now, you have to tell the user some way or another on which datatype members you have to use the function.The proposal
My proposal is using object de-structuring, seen in other programming languages, to not only give datatype safety but also avoid writing long function calls.
Assume we have a struct for a figure
And we have a function that takes some of the members of this
figure
. You may attempt to just doBut we now have all of those members that we don't need in the cache, not good. However, if we apply a hypothetical syntax for de-structing types, we could just do, for example
We would tell the cpp2 that we only need the
x
andy
member from the typefigure
. Now instead of callingsome_calc(fig.x, fig.y, etc...)
we just could to simply dosome_calc(fig)
and let cpp2 do all the work for us. Also, we can tell which behaviors these parameters could have, for example, we could putinout
for them.Conclusion
Other programming languages such as Typescript already has this feature, but as far as I know, it doesn't have type safety (I could be wrong). It's something I would love to have to keep my life a little bit simpler.
Beta Was this translation helpful? Give feedback.
All reactions