-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate constructors for mir.struct
protobufs
#409
Comments
@xosmig does this make sense to you? Intuitively this seems like something very easy to implement by slightly extending the current codegen tools. Is that the case? |
Doesn't Another note:
Yes, I believe it should be relatively easy to implement. I guess github copilot should be able to generate the necessary code :) |
Ah yes you're right, |
Same way as the
mir.event
-anotated protobuf messages get a generated constructor,mir.struct
-annotated protobufs should also have constructor functions for the corresponding generated Go types. Ideally, an event would automatically be as struct as well, and the corresponding types and constructors would be generated too.For example, the following protobuf
would generate code such that a programmer can instantiate events and structs like this:
Here the
fooStruct
would just be a plain data structure, whileevt
would wrap it in the corresponding hierarchical event structure. ForbarStruct
, no event exists.The added value of generating constructor for structs is not just convenience, but also a more robust programming model, as the constructor always forces the programmer to specify all the fields. When, at some point, the definition of a protobuf message changes (e.g. a new field is added), it is very easy to locate its usage in the code (and add the extra value), as the compiler would enforce it. On the other hand, if the objects were constructed as simple struct literals, the compiler would simply implicitly (and silently) fill in the zero value for any new fields and the programmer runs a high risk of not updating all their code properly.
The text was updated successfully, but these errors were encountered: