Specifying a default value for a member of a struct #4738
Unanswered
mitchgrout
asked this question in
Q&A
Replies: 1 comment
-
That sounds like a good idea to me as a suggestion. |
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
-
As I understand, if a member of a struct is not explicitly initialized, it will be set to its default value. However, as far as I can tell there is no way to set a new default value for a member, as the following is illegal:
My take is that the default value for some types, including user-defined types, might not always be desirable. For example, I might want to have
export enum Accidental { flat, neutral, sharp }
. However, if I used this within a struct and forgot to explicitly initialize the field, then it would default toflat
as it is the first element in the enum. My preference however would be that it should beneutral
.Of course, I could resolve this by re-ordering my enum, but it feels as though there should be some flexibility here?
Beta Was this translation helpful? Give feedback.
All reactions