Add a compile time table construct/ X-macro (C) approximation #5838
Closed
alexgorcrom
started this conversation in
Ideas/Requests
Replies: 1 comment
-
|
I understand the desire for the macro, but it's not going to happen, sorry. The best solution at the moment is doing the enumerated array thing, which does at least make sure you cover all the cases. |
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
Uh oh!
There was an error while loading. Please reload this page.
-
Idea
One of the very useful C constructs that can be created using macros is the so called X-macro that allows the maintenance of many related pieces of data effectively in a single table in a single place and if new data needs to be added this is easily done and will then be propagated across the whole codebase without having to explicitly do much (in most cases). An example of this sort of pattern would be something like:
With the above if the North West direction is added the enums and functions that depend on the X macro will be automatically updated and the new values will be consistently managed. This pattern is incredibly useful especially in embedded situations.
If this were to be turned into a language feature this would map closest to a compile time relational table.
I would imagine that in Odin this could look something like:
In addition to the user defined fields there would be two implicit ones
KEYandNAME,KEYbeing the entry id andNAMEbeing the string of the table key, i.e.NorthandSouthin the above example. The relevant column would then be accessed something like:.North.file_name. Many of the existing constructs could be used as if this were anenum.Issues/Similarities
Enums
In the case where a user does not specify any additional fields (kind of against the point of such a construct) this basically degenerates into an enum
Enumerated Arrays
Functionally this end up providing the same resulting behaviour as the enumerated arrays and is more restricted - in particular it cannot be partial (this I would consider a benefit) as it does not make sense to have a partial table of this type.
I guess this would essentially be syntactic sugar for:
Given that it would be syntactic sugar of the above, it means that the same rules could be used to extend it by a user of the table as the enumerated arrays, i.e.:
The main benefits that I see of such a type would be that when the type is passed around the "default" enumerated array associated with the internal enum would be implicit when fields are being accessed from a
proc()and it keeps all the data in once place, quite tightly packed, whereas the expansion of it is more code to look through (not the biggest of issues - granted)Beta Was this translation helpful? Give feedback.
All reactions